Java(eclipse)連線MySQL8.0以上版本資料庫方式
阿新 • • 發佈:2020-10-26
求強連通。例題:
【NOIP2015】資訊傳遞
#pragma GCC optimize(3) #pragma GCC optimize(2) #include <iostream> #include <string> #include <cstdio> #include <algorithm> #include <cstring> #include <cmath> #include <queue> #include <functional> #include <vector> #include<map> #include <set> #include <stack> #include <fstream> #include <sstream> #include <unordered_map> #define FT(a, b) memset(a, b, sizeof(a)) #define FAT(a) memset(a, 0, sizeof(a)) using namespace std; typedef long long ll; const int N = 2e5 + 10; const int M = 3e5 + 100; const int INF = 0x3f3f3f3f; const int mod = 1e9 + 7; int n, m, k, x, y, t; vector<int> e[N]; int a[N], dfn[N], low[N], idx = 0; stack<int> st; bool vis[N]; int ans = INF; void tarjan(int now) { dfn[now] = low[now] = ++idx; st.push(now); vis[now] = 1; for (int it : e[now]) {if (!dfn[it]) { tarjan(it); low[now] = min(low[now], low[it]); } else if (vis[now]) { low[now] = min(low[now], dfn[it]); } } if (low[now] == dfn[now]) { int cnt = 0; while (st.size()) { int x = st.top(); st.pop(); vis[x] = 0; cnt++; if (x == now) break; } if (cnt > 1) ans = min(ans, cnt); } } void solve() { scanf("%d", &n); FAT(dfn); FAT(vis); FAT(low); for (int i = 1; i <= n; ++i) { int u; scanf("%d", &u); e[i].push_back(u); } for (int i = 1; i <= n; ++i) if (!dfn[i]) tarjan(i); printf("%d\n", ans); } int main() { #ifdef ONLINE_JUDGE #else freopen("/home/wjy/code/in.txt", "r", stdin); // freopen("/home/wjy/code/ans1.txt","w", stdout); #endif ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); int T = 1; // scanf("%d", &T); // cin >> T; // time_t begin, end; // begin = clock(); while (T--) { solve(); } // end = clock(); // double ret = double(end - begin) / CLOCKS_PER_SEC; return 0; }