D、华华和月月逛公园
tarjan算法:跑有向图的强连通分量算法
1、新加节点,直接加新的编号
2、对于来过的节点,最小编号更新为可以这个强连通分量中序最小的值。
3、对于这个题目来说,只需要判断是不是2个强连通分量之间,是不是不同值,如果子节点的序小于自己值,说明这条路是连接2个强连通分量的必经之路,必不能省略。
4、最终答案就是总边数减掉必经的道路
#include <bits/stdc++.h> #pragma GCC optimize(2) #pragma GCC optimize(3) using namespace std; #define js ios::sync_with_stdio(false);cin.tie(0); cout.tie(0) typedef long long ll; const ll MOD = 1e9 + 7; inline ll read() { ll s = 0, w = 1; char ch = getchar(); while (ch < 48 || ch > 57) { if (ch == '-') w = -1; ch = getchar(); } while (ch >= 48 && ch <= 57) s = (s << 1) + (s << 3) + (ch ^ 48), ch = getchar(); return s * w; } inline void write(ll x) { if (!x) { putchar('0'); return; } char F[200]; ll tmp = x > 0 ? x : -x; if (x < 0)putchar('-'); int cnt = 0; while (tmp > 0) { F[cnt++] = tmp % 10 + '0'; tmp /= 10; } while (cnt > 0)putchar(F[--cnt]); } inline ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; } ll qpow(ll a, ll b) { ll ans = 1; while (b) { if (b & 1) ans *= a; b >>= 1; a *= a; } return ans; } ll qpow(ll a, ll b, ll mod) { ll ans = 1; while (b) { if (b & 1)(ans *= a) %= mod; b >>= 1; (a *= a) %= mod; }return ans % mod; } inline int lowbit(int x) { return x & (-x); } const int N = 1e5 + 7; //节点数 const int M = 3e5 + 7; //路径数 const int INF = 0x3f3f3f3f; int head[N], tot = 0;//前向星变量 struct Node { //int u; //起点 //int w; //权值 int v, next; } edge[M << 1]; void add(int u, int v) { tot++; //edge[tot].u = u; edge[tot].v = v; //edge[tot].w = w; edge[tot].next = head[u]; head[u] = tot; } int n, m, c, dfn[N], low[N], tmp; void tarjan(int x, int fa) { dfn[x] = low[x] = ++c; for (int i = head[x]; i; i = edge[i].next) { int y = edge[i].v; if (!dfn[y]) { tarjan(y, x); low[x] = min(low[x], low[y]); if (low[y] > dfn[x]) ++tmp; } else if (y != fa) low[x] = min(low[x], dfn[y]); } } int main() { n = read(), m = read(); for (int i = 1; i <= m; ++i) { int u = read(), v = read(); add(u, v); add(v, u); } tarjan(1, 0); write(m - tmp); return 0; }
E、数字比较
我竟然会用快速幂……究竟是有多思想固化了,模板敲多了,思维没跟上。
这个题目直接把左右取个对数,再移个项,除一下过来,就可以得到一个和的式子比较这两个式子大小即可出结果。
#include <bits/stdc++.h> #pragma GCC optimize(2) #pragma GCC optimize(3) using namespace std; #define js ios::sync_with_stdio(false);cin.tie(0); cout.tie(0) typedef long long ll; const ll MOD = 1e9 + 7; inline ll read() { ll s = 0, w = 1; char ch = getchar(); while (ch < 48 || ch > 57) { if (ch == '-') w = -1; ch = getchar(); } while (ch >= 48 && ch <= 57) s = (s << 1) + (s << 3) + (ch ^ 48), ch = getchar(); return s * w; } inline void write(ll x) { if (!x) { putchar('0'); return; } char F[200]; ll tmp = x > 0 ? x : -x; if (x < 0)putchar('-'); int cnt = 0; while (tmp > 0) { F[cnt++] = tmp % 10 + '0'; tmp /= 10; } while (cnt > 0)putchar(F[--cnt]); } inline ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; } ll qpow(ll a, ll b) { ll ans = 1; while (b) { if (b & 1) ans *= a; b >>= 1; a *= a; } return ans; } ll qpow(ll a, ll b, ll mod) { ll ans = 1; while (b) { if (b & 1)(ans *= a) %= mod; b >>= 1; (a *= a) %= mod; }return ans % mod; } inline int lowbit(int x) { return x & (-x); } const double eps = 1e-7; int main() { js; double a, b; cin >> a >> b; double ans1 = log(a) / log(b), ans2 = a / b; if (fabs(ans1 - ans2) < eps) cout << "="; else if (ans1 > ans2) cout << ">"; else cout << "<"; return 0; }