題目描述
在幻想鄉,上白澤慧音是以知識淵博聞名的老師。春雪異變導致人間之里的很多道路都被大雪堵塞,使有的學生不能順利地到達慧音所在的村莊。因此慧音決定換一個能夠聚集最多人數的村莊作為新的教學地點。人間之里由N個村莊(編號為1..N)和M條道路組成,道路分為兩種一種為單向通行的,一種為雙向通行的,分別用1和2來標記。如果存在由村莊A到達村莊B的通路,那么我們認為可以從村莊A到達村莊B,記為(A,B)。當(A,B)和(B,A)同時滿足時,我們認為A,B是絕對連通的,記為
輸入輸出格式
輸入格式:
第1行:兩個正整數N,M 第2..M+1行:每行三個正整數a,b,t, t = 1表示存在從村莊a到b的單向道路,t = 2表示村莊a,b之間存在雙向通行的道路。保證每條道路只出現一次。
輸出格式:
第1行: 1個整數,表示最大的絕對連通區域包含的村莊個數。 第2行:若干個整數,依次輸出最大的絕對連通區域所包含的村莊編號。
輸入輸出樣例
輸入樣例#1:
5 5 1 2 1 1 3 2 2 4 2 5 1 2 3 5 1
輸出樣例#1:
3 1 3 5
說明
對于60%的數據:N <= 200且M <= 10,000 對于100%的數據:N <= 5,000且M <= 50,000
題解
怎么說呢,看到雙向邊的我否定了強連通分量,然并卵我還是轉tarjan接著1A了 這是第一道爬蟲抓下來的洛谷題目介紹,感覺反而更麻煩了。。
Code
#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <ctime>#include <iostream>#include <algorithm>#include <string>#include <vector>#include <deque>#include <list>#include <set>#include <map>#include <stack>#include <queue>#include <numeric>#include <iomanip>#include <bitset>#include <sstream>#include <fstream>#define debug puts("-----")#define rep(i, st, ed) for (int i = st; i <= ed; i += 1)#define drp(i, st, ed) for (int i = st; i >= ed; i -= 1)#define fill(x, t) memset(x, t, sizeof(x))#define min(x, y) x<y?x:y#define max(x, y) x>y?x:y#define PI (acos(-1.0))#define EPS (1e-8)#define INF (1<<30)#define ll long long#define db double#define ld long double#define N 5001#define E N * 16 + 1#define MOD 100000007#define L 255using namespace std;struct edge{ int x, y, w, next;}e[E];int inStack[N], size[N], scc[N], dfn[N], low[N], ls[N];int cnt;stack<int>s;inline int read(){ int x = 0, v = 1; char ch = getchar(); while (ch < '0' || ch > '9'){ if (ch == '-'){ v = -1; } ch = getchar(); } while (ch <= '9' && ch >= '0'){ x = (x << 1) + (x << 3) + ch - '0'; ch = getchar(); } return x * v;}inline int addEdge(int &cnt, const int &x, const int &y, const int &w = 1){ e[++ cnt] = (edge){x, y, w, ls[x]}; ls[x] = cnt; return 0;}inline int dfs(int now){ dfn[now] = low[now] = ++ cnt; inStack[now] = 1; s.push(now); for (int i = ls[now]; i; i = e[i].next){ if (!dfn[e[i].y]){ dfs(e[i].y); low[now] = min(low[now], low[e[i].y]); }else if (inStack[e[i].y]){ low[now] = min(low[now], dfn[e[i].y]); } } if (low[now] == dfn[now]){ scc[0] += 1; for (int tmp = 0; tmp != now; ){ tmp = s.top(); s.pop(); scc[tmp] = scc[0]; inStack[tmp] = 0; size[scc[0]] += 1; } }}inline int tarjan(const int &n){ fill(inStack, 0); fill(size, 0); fill(dfn, 0); fill(low, 0); cnt = 0; rep(i, 1, n){ if (!dfn[i]){ dfs(i); } }}int main(void){ int n = read(), m = read(); int edgeCnt = 1; rep(i, 1, m){ int x = read(), y = read(), opt = read() - 1; if (opt){ addEdge(edgeCnt, x, y); addEdge(edgeCnt, y, x); }else{ addEdge(edgeCnt, x, y); } } tarjan(n); int ans = 0; rep(i, 1, n){ ans = max(ans, size[scc[i]]); }