#include<bits/stdc++.h> #include<cstdio> using namespace std; #define R(p, k) for(int i = p; i <= k; i++) #define RI(i, p, k) for(int i = p; i <= k; i++) #define IR(k, p) for(int i = k; i >= p; i--) #define IRI(i, p, k) for(int i = k; i >= p; i--) #define DEB 0 #if DEB #define EPRLL(a) fprintf(stderr, "%s %lld ", #a, a); #define EPR(a) fprintf(stderr, "%s %d ", #a, a); #define EPRN fprintf(stderr, "\n"); #else #define EPRLL(a) #define EPR(a) #define EPRN #endif const int MN=1e4+5; int n, m, t=0, nrcons=1, ndone=0; vector<int> vin[MN], vout[MN], antie[MN], auxe; int outdeg[MN], aindeg[MN], parent[MN], dead[MN], edgesto[MN]; int done[MN], vis[MN], countae[MN]; queue<int> todo; void add(int i){ if(outdeg[i]==0 && aindeg[i]==0){ todo.push(i); ndone++; EPR(i); } } void dfs(int v, int par, int nr, int newnr){ if(done[v]==1 || vis[v] > newnr) return; /*t++; //!!!!!!!!!!!!!!!!!!!!!!!!!!! if(t>300) return;*/ EPR(v); parent[v]=par; vis[v]=nr; R(0, (signed)antie[v].size()-1){ int k=antie[v][i]; if(!dead[k]){ auxe.push_back(k); } } R(0, (signed)vin[v].size()-1){ int v2=vin[v][i]; dfs(v2, par, nr, newnr); } R(0, (signed)vout[v].size()-1){ int v2=vout[v][i]; dfs(v2, par, nr, newnr); } } void upd_cons(int v, int v2, int newnr){ t++; EPR(v2); nrcons++; auxe.clear(); dfs(v2, v, nrcons, newnr); RI(j, 0, (signed)auxe.size()-1){ int k = auxe[j]; countae[k]++; } RI(j, 0, (signed)auxe.size()-1){ int k = auxe[j]; if(countae[k]&1 && !dead[k]){ int v2=edgesto[k]; aindeg[v2]--; add(v2); dead[k]=1; } } } int main(){ int v1, v2; char s[5]; scanf("%d%d", &n, &m); R(1, m){ scanf("%d%d%s", &v1, &v2, s); if(s[0]=='T'){ outdeg[v1]++; vin[v2].push_back(v1); vout[v1].push_back(v2); } else{ aindeg[v2]++; antie[v1].push_back(i); antie[v2].push_back(i); edgesto[i]=v2; } } R(1, n){ add(i); EPR(outdeg[i]); EPR(aindeg[i]); } while(!todo.empty()){ /*t++; if(t>300) break;*/ int v = todo.front(); todo.pop(); done[v]=1; EPRN; EPR(v); EPRN; R(0, (signed)antie[v].size()-1){ int k=antie[v][i]; EPR(k); if(!dead[k]){ int v2=edgesto[k]; aindeg[v2]--; add(v2); dead[k]=1; } } int newnr=nrcons; if(parent[v]==0){ R(1, n){ upd_cons(v, i, newnr); } } R(0, (signed)vin[v].size()-1){ int v2 = vin[v][i]; outdeg[v2]--; add(v2); upd_cons(v, vin[v][i], newnr); } } if(ndone!=n){ printf("NIE\n"); } else{ R(1, n) printf("%d\n", parent[i]); //printf("TAK\n"); } return 0; }
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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | #include<bits/stdc++.h> #include<cstdio> using namespace std; #define R(p, k) for(int i = p; i <= k; i++) #define RI(i, p, k) for(int i = p; i <= k; i++) #define IR(k, p) for(int i = k; i >= p; i--) #define IRI(i, p, k) for(int i = k; i >= p; i--) #define DEB 0 #if DEB #define EPRLL(a) fprintf(stderr, "%s %lld ", #a, a); #define EPR(a) fprintf(stderr, "%s %d ", #a, a); #define EPRN fprintf(stderr, "\n"); #else #define EPRLL(a) #define EPR(a) #define EPRN #endif const int MN=1e4+5; int n, m, t=0, nrcons=1, ndone=0; vector<int> vin[MN], vout[MN], antie[MN], auxe; int outdeg[MN], aindeg[MN], parent[MN], dead[MN], edgesto[MN]; int done[MN], vis[MN], countae[MN]; queue<int> todo; void add(int i){ if(outdeg[i]==0 && aindeg[i]==0){ todo.push(i); ndone++; EPR(i); } } void dfs(int v, int par, int nr, int newnr){ if(done[v]==1 || vis[v] > newnr) return; /*t++; //!!!!!!!!!!!!!!!!!!!!!!!!!!! if(t>300) return;*/ EPR(v); parent[v]=par; vis[v]=nr; R(0, (signed)antie[v].size()-1){ int k=antie[v][i]; if(!dead[k]){ auxe.push_back(k); } } R(0, (signed)vin[v].size()-1){ int v2=vin[v][i]; dfs(v2, par, nr, newnr); } R(0, (signed)vout[v].size()-1){ int v2=vout[v][i]; dfs(v2, par, nr, newnr); } } void upd_cons(int v, int v2, int newnr){ t++; EPR(v2); nrcons++; auxe.clear(); dfs(v2, v, nrcons, newnr); RI(j, 0, (signed)auxe.size()-1){ int k = auxe[j]; countae[k]++; } RI(j, 0, (signed)auxe.size()-1){ int k = auxe[j]; if(countae[k]&1 && !dead[k]){ int v2=edgesto[k]; aindeg[v2]--; add(v2); dead[k]=1; } } } int main(){ int v1, v2; char s[5]; scanf("%d%d", &n, &m); R(1, m){ scanf("%d%d%s", &v1, &v2, s); if(s[0]=='T'){ outdeg[v1]++; vin[v2].push_back(v1); vout[v1].push_back(v2); } else{ aindeg[v2]++; antie[v1].push_back(i); antie[v2].push_back(i); edgesto[i]=v2; } } R(1, n){ add(i); EPR(outdeg[i]); EPR(aindeg[i]); } while(!todo.empty()){ /*t++; if(t>300) break;*/ int v = todo.front(); todo.pop(); done[v]=1; EPRN; EPR(v); EPRN; R(0, (signed)antie[v].size()-1){ int k=antie[v][i]; EPR(k); if(!dead[k]){ int v2=edgesto[k]; aindeg[v2]--; add(v2); dead[k]=1; } } int newnr=nrcons; if(parent[v]==0){ R(1, n){ upd_cons(v, i, newnr); } } R(0, (signed)vin[v].size()-1){ int v2 = vin[v][i]; outdeg[v2]--; add(v2); upd_cons(v, vin[v][i], newnr); } } if(ndone!=n){ printf("NIE\n"); } else{ R(1, n) printf("%d\n", parent[i]); //printf("TAK\n"); } return 0; } |