Niestety, nie byliśmy w stanie w pełni poprawnie wyświetlić tego pliku, ponieważ nie jest zakodowany w UTF-8.
Możesz pobrać ten plik i spróbować otworzyć go samodzielnie.
// pozwoli�em sobie po�yczy� kod na LCA z topcodera // http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=lowestCommonAncestor#Another%20easy%20solution%20in%20O%28N%20logN,%20O%28logN%29 // #include <list> #include <cstdlib> #include <algorithm> #include <vector> #include <cstdio> #include <queue> #define max(a,b) (((a)>(b))?(a):(b)) #define min(a,b) (((a)<(b))?(a):(b)) using namespace std; int ILE[200005]; int T[200005]; vector<int> R1; vector<int> R2; long long int WYNIK; int N, M, K; vector<int> FOO[200005]; vector<int> start; int a,b; int vis[200005]; int nrk[200005]; int L[200005]; vector<int> sas[200005]; queue<int> bbq; int tablica[200005][20]; int TABLICA[200005][20]; void BFS(int st) { vis[st] = st; L[st] = 0; bbq.push(st); while(!bbq.empty()) { int u = bbq.front(); bbq.pop(); vis[u] = st; for (int i = 0;i<sas[u].size();++i) { L[sas[u][i]]=L[u]+1; bbq.push(sas[u][i]); } } } int query(int p,int q) { int tmp, log, i; if ( L[p] < L[q] ) { tmp = p; p = q; q = tmp; } for (log = 1; (1<<log) <= L[p]; ++log); --log; int nk=0; for (i = log; i >=0;--i) if ( L[p] - (1<<i) >= L[q]) { nk = TABLICA[p][i]; p = tablica[p][i]; } if (p==q) return nk; //L[q]; for ( i = log; i>=0; --i) if ( tablica[p][i] != -1 && tablica[p][i] != tablica[q][i]) { p=tablica[p][i]; q=tablica[q][i]; } return max(nrk[p],nrk[q]);//L[T[p]]; //if (p<q) } int main() { scanf("%d %d %d", &N, &M, &K); for (int i = 1; i<=N;++i) scanf("%d",ILE+i); for (int i = 0; i<M;++i) { scanf("%d %d", &a, &b); T[a] = b; sas[b].push_back(a); nrk[a] = i; } for (int i =1;i<=N;++i) { if ( vis[i] ) continue; int u = i; while( T[u] != 0 ) u = T[u]; BFS(u); } for (int i = 1; i<=N;++i) for (int j = 0; (1<<j) < N; ++j ) tablica[i][j] = -1; for (int i = 1; i<=N; ++i) { tablica[i][0] = T[i]; TABLICA[i][0] = nrk[i]; } for (int j = 1; (1<<j) < N; ++j) for (int i = 1; i<=N; ++i) if (tablica[i][j-1] != -1) { TABLICA[i][j] = TABLICA[tablica[i][j-1]][j-1]; tablica[i][j] = tablica[tablica[i][j-1]][j-1]; } for (int i = 0;i<K;++i) { int r1,r2; scanf("%d %d",&r1,&r2); if ( vis[r1] != vis[r2] ) continue; int LL = query(r1,r2); FOO[LL].push_back( R1.size() ); R1.push_back(r1); R2.push_back(r2); } for (int i = 0; i<=M;++i) { //while (!FOO[i].empty()) for (int j = 0; j<FOO[i].size();++j) { //int ind = FOO[i].front(); int ind = FOO[i][j]; //FOO[i].pop(); long long int m = min( ILE[R1[ind]],ILE[R2[ind]]); WYNIK += 2*m; ILE[R1[ind]] -= m; ILE[R2[ind]] -= m; } } printf("%lld",WYNIK); 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 162 163 164 | // pozwoli�em sobie po�yczy� kod na LCA z topcodera // http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=lowestCommonAncestor#Another%20easy%20solution%20in%20O%28N%20logN,%20O%28logN%29 // #include <list> #include <cstdlib> #include <algorithm> #include <vector> #include <cstdio> #include <queue> #define max(a,b) (((a)>(b))?(a):(b)) #define min(a,b) (((a)<(b))?(a):(b)) using namespace std; int ILE[200005]; int T[200005]; vector<int> R1; vector<int> R2; long long int WYNIK; int N, M, K; vector<int> FOO[200005]; vector<int> start; int a,b; int vis[200005]; int nrk[200005]; int L[200005]; vector<int> sas[200005]; queue<int> bbq; int tablica[200005][20]; int TABLICA[200005][20]; void BFS(int st) { vis[st] = st; L[st] = 0; bbq.push(st); while(!bbq.empty()) { int u = bbq.front(); bbq.pop(); vis[u] = st; for (int i = 0;i<sas[u].size();++i) { L[sas[u][i]]=L[u]+1; bbq.push(sas[u][i]); } } } int query(int p,int q) { int tmp, log, i; if ( L[p] < L[q] ) { tmp = p; p = q; q = tmp; } for (log = 1; (1<<log) <= L[p]; ++log); --log; int nk=0; for (i = log; i >=0;--i) if ( L[p] - (1<<i) >= L[q]) { nk = TABLICA[p][i]; p = tablica[p][i]; } if (p==q) return nk; //L[q]; for ( i = log; i>=0; --i) if ( tablica[p][i] != -1 && tablica[p][i] != tablica[q][i]) { p=tablica[p][i]; q=tablica[q][i]; } return max(nrk[p],nrk[q]);//L[T[p]]; //if (p<q) } int main() { scanf("%d %d %d", &N, &M, &K); for (int i = 1; i<=N;++i) scanf("%d",ILE+i); for (int i = 0; i<M;++i) { scanf("%d %d", &a, &b); T[a] = b; sas[b].push_back(a); nrk[a] = i; } for (int i =1;i<=N;++i) { if ( vis[i] ) continue; int u = i; while( T[u] != 0 ) u = T[u]; BFS(u); } for (int i = 1; i<=N;++i) for (int j = 0; (1<<j) < N; ++j ) tablica[i][j] = -1; for (int i = 1; i<=N; ++i) { tablica[i][0] = T[i]; TABLICA[i][0] = nrk[i]; } for (int j = 1; (1<<j) < N; ++j) for (int i = 1; i<=N; ++i) if (tablica[i][j-1] != -1) { TABLICA[i][j] = TABLICA[tablica[i][j-1]][j-1]; tablica[i][j] = tablica[tablica[i][j-1]][j-1]; } for (int i = 0;i<K;++i) { int r1,r2; scanf("%d %d",&r1,&r2); if ( vis[r1] != vis[r2] ) continue; int LL = query(r1,r2); FOO[LL].push_back( R1.size() ); R1.push_back(r1); R2.push_back(r2); } for (int i = 0; i<=M;++i) { //while (!FOO[i].empty()) for (int j = 0; j<FOO[i].size();++j) { //int ind = FOO[i].front(); int ind = FOO[i][j]; //FOO[i].pop(); long long int m = min( ILE[R1[ind]],ILE[R2[ind]]); WYNIK += 2*m; ILE[R1[ind]] -= m; ILE[R2[ind]] -= m; } } printf("%lld",WYNIK); return 0; } |