#include<bits/stdc++.h> #define PII pair<int,int> #define f first #define s second #define VI vector<int> #define LL long long #define MP make_pair #define LD long double #define PB push_back #define ALL(V) V.begin(),V.end() #define abs(x) max((x),-(x)) #define PDD pair<LD,LD> #define VPII vector< PII > #define siz(V) ((int)V.size()) #define FOR(x, b, e) for(int x=b;x<=(e);x++) #define FORD(x, b, e) for(int x=b;x>=(e);x--) #define REP(x, n) for(int x=0;x<(n);x++) #define mini(a,b) a=min(a,b) #define maxi(a,b) a=max(a,b) using namespace std; const int MXM = 10001; const int MXN = 1003; int a[MXM]; int b[MXM]; int T[MXM]; bool noroot[MXN]; int res[MXN]; int n, m; int find_root(bitset<MXM> &edges, bitset<MXN> &vert) { FOR(i, 1, n)noroot[i] = 0; REP(i, m) { if(!edges[i])continue; if(T[i] == 0) noroot[b[i]] = 1; else noroot[a[i]] = 1; } FOR(i, 1, n) if(vert[i] && noroot[i] == 0) return i; puts("NIE"); exit(0); } int f[MXN]; void clearFU() { FOR(i, 1, n) f[i] = i; } int find(int x) { return x == f[x] ? x : f[x] = find(f[x]); } void unio(int a, int b) { f[find(a)] = find(b); } void go(bitset<MXM> edges, bitset<MXN> vert, int fat) { int v = find_root(edges, vert); res[v] = fat; clearFU(); REP(i, m) { if(!edges[i])continue; if(a[i] == v || b[i] == v)continue; if(T[i] == 1) unio(a[i], b[i]); } map<int, bitset<MXM>> newedges; map<int, bitset<MXN>> newvert; REP(i, m) { if(!edges[i])continue; if(find(a[i]) == find(b[i])) { newedges[find(a[i])][i] = 1; } } FOR(i, 1, n) { if(!vert[i])continue; if(i == v)continue; newvert[find(i)][i] = 1; } for(auto it:newvert) { go(newedges[it.f], it.s, v); } } int main() { scanf("%d%d", &n, &m); bitset<MXM> start; bitset<MXN> V; REP(i, m) { char ch; scanf("%d%d %c", &a[i], &b[i], &ch); T[i] = ch == 'T'; start[i] = 1; } FOR(i, 1, n) V[i] = 1; go(start, V, 0); //puts("TAK"); FOR(i, 1, n) printf("%d\n", res[i]); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | #include<bits/stdc++.h> #define PII pair<int,int> #define f first #define s second #define VI vector<int> #define LL long long #define MP make_pair #define LD long double #define PB push_back #define ALL(V) V.begin(),V.end() #define abs(x) max((x),-(x)) #define PDD pair<LD,LD> #define VPII vector< PII > #define siz(V) ((int)V.size()) #define FOR(x, b, e) for(int x=b;x<=(e);x++) #define FORD(x, b, e) for(int x=b;x>=(e);x--) #define REP(x, n) for(int x=0;x<(n);x++) #define mini(a,b) a=min(a,b) #define maxi(a,b) a=max(a,b) using namespace std; const int MXM = 10001; const int MXN = 1003; int a[MXM]; int b[MXM]; int T[MXM]; bool noroot[MXN]; int res[MXN]; int n, m; int find_root(bitset<MXM> &edges, bitset<MXN> &vert) { FOR(i, 1, n)noroot[i] = 0; REP(i, m) { if(!edges[i])continue; if(T[i] == 0) noroot[b[i]] = 1; else noroot[a[i]] = 1; } FOR(i, 1, n) if(vert[i] && noroot[i] == 0) return i; puts("NIE"); exit(0); } int f[MXN]; void clearFU() { FOR(i, 1, n) f[i] = i; } int find(int x) { return x == f[x] ? x : f[x] = find(f[x]); } void unio(int a, int b) { f[find(a)] = find(b); } void go(bitset<MXM> edges, bitset<MXN> vert, int fat) { int v = find_root(edges, vert); res[v] = fat; clearFU(); REP(i, m) { if(!edges[i])continue; if(a[i] == v || b[i] == v)continue; if(T[i] == 1) unio(a[i], b[i]); } map<int, bitset<MXM>> newedges; map<int, bitset<MXN>> newvert; REP(i, m) { if(!edges[i])continue; if(find(a[i]) == find(b[i])) { newedges[find(a[i])][i] = 1; } } FOR(i, 1, n) { if(!vert[i])continue; if(i == v)continue; newvert[find(i)][i] = 1; } for(auto it:newvert) { go(newedges[it.f], it.s, v); } } int main() { scanf("%d%d", &n, &m); bitset<MXM> start; bitset<MXN> V; REP(i, m) { char ch; scanf("%d%d %c", &a[i], &b[i], &ch); T[i] = ch == 'T'; start[i] = 1; } FOR(i, 1, n) V[i] = 1; go(start, V, 0); //puts("TAK"); FOR(i, 1, n) printf("%d\n", res[i]); } |