# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
297244 | 2020-09-11T12:14:02 Z | Lawliet | Pipes (BOI13_pipes) | C++17 | 402 ms | 27640 KB |
#include <bits/stdc++.h> using namespace std; typedef long long int lli; const int MAXN = 100010; const int MAXM = 500010; int n, m; int v[MAXN]; int degree[MAXN]; lli ans[MAXM]; lli curSum[MAXN]; bool mark[MAXN]; vector<int> adj[MAXN]; vector<int> indEdge[MAXN]; queue<int> q; void add(int cur) { q.push(cur); mark[cur] = true; } int nextIndEdge(int cur) { for(int i = 0 ; i < (int)adj[cur].size() ; i++) if( !mark[ adj[cur][i] ] ) return i; return -1; } lli getDiff(int cur) { return v[cur] - curSum[cur]; } void topologicalSorting() { for(int i = 1 ; i <= n ; i++) if( degree[i] == 1 ) add( i ); while( !q.empty() ) { int cur = q.front(); q.pop(); int nxtInd = nextIndEdge( cur ); int anc = adj[cur][nxtInd]; int ind = indEdge[cur][nxtInd]; ans[ind] = getDiff(cur); curSum[cur] += ans[ind]; curSum[anc] += ans[ind]; if( --degree[anc] == 1 ) add( anc ); } } void addEdge(int U, int V, int i) { adj[U].push_back( V ); adj[V].push_back( U ); indEdge[U].push_back( i ); indEdge[V].push_back( i ); degree[U]++; degree[V]++; } void notUnique() { printf("0\n"); exit(0); } void printAnswer() { for(int i = 1 ; i <= m ; i++) printf("%lld\n",2*ans[i]); } void findAnswerCycle() { int rootCycle; for(int i = 1 ; i <= n ; i++) if( !mark[i] ) rootCycle = i; int cur = rootCycle; vector<int> cycleNodes; vector<int> cycleEdges; while( !mark[cur] ) { mark[cur] = true; cycleNodes.push_back( cur ); int nxtInd = nextIndEdge( cur ); if( nxtInd == -1 ) break; cycleEdges.push_back( indEdge[cur][nxtInd] ); cur = adj[cur][nxtInd]; } for(int i = 0 ; i < (int)adj[cur].size() ; i++) if( adj[cur][i] == rootCycle ) cycleEdges.push_back( indEdge[cur][i] ); lli sumCycle = 0; int szCycle = (int)cycleNodes.size(); if( szCycle%2 == 0 ) notUnique(); for(int i = 0 ; i < szCycle ; i++) sumCycle += getDiff( cycleNodes[i] ); sumCycle /= 2; lli sumPrefix[] = { 0 , 0 }; lli sumSuffix[] = { 0 , 0 }; for(int i = 0 ; i < szCycle ; i++) sumSuffix[i%2] += getDiff( cycleNodes[i] ); for(int i = 0 ; i < szCycle ; i++) { sumPrefix[i%2] += getDiff( cycleNodes[i] ); sumSuffix[i%2] -= getDiff( cycleNodes[i] ); ans[ cycleEdges[i] ] = sumCycle - sumPrefix[1 - i%2] - sumSuffix[i%2]; } } int main() { scanf("%d %d",&n,&m); for(int i = 1 ; i <= n ; i++) scanf("%d",&v[i]); for(int i = 1 ; i <= m ; i++) { int U, V; scanf("%d %d",&U,&V); addEdge( U , V , i ); } if( m > n ) notUnique(); if( m == n - 1 ) addEdge( 1 , 1 , n ); topologicalSorting(); if( m == n ) findAnswerCycle(); printAnswer(); }
Compilation message
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 4 ms | 4992 KB | Output is correct |
2 | Correct | 4 ms | 4992 KB | Output is correct |
3 | Correct | 5 ms | 5120 KB | Output is correct |
4 | Correct | 135 ms | 16128 KB | Output is correct |
5 | Correct | 4 ms | 4992 KB | Output is correct |
6 | Correct | 4 ms | 5120 KB | Output is correct |
7 | Correct | 4 ms | 4992 KB | Output is correct |
8 | Correct | 4 ms | 4992 KB | Output is correct |
9 | Correct | 4 ms | 5120 KB | Output is correct |
10 | Correct | 4 ms | 5120 KB | Output is correct |
11 | Correct | 5 ms | 5120 KB | Output is correct |
12 | Correct | 4 ms | 5120 KB | Output is correct |
13 | Correct | 100 ms | 13848 KB | Output is correct |
14 | Correct | 122 ms | 15480 KB | Output is correct |
15 | Correct | 128 ms | 16264 KB | Output is correct |
16 | Correct | 109 ms | 14584 KB | Output is correct |
17 | Correct | 132 ms | 16088 KB | Output is correct |
18 | Correct | 155 ms | 16088 KB | Output is correct |
19 | Correct | 135 ms | 15784 KB | Output is correct |
20 | Correct | 4 ms | 4992 KB | Output is correct |
21 | Correct | 4 ms | 5120 KB | Output is correct |
22 | Correct | 133 ms | 16216 KB | Output is correct |
23 | Correct | 97 ms | 13816 KB | Output is correct |
24 | Correct | 128 ms | 16216 KB | Output is correct |
25 | Correct | 104 ms | 14456 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 4 ms | 4992 KB | Output is correct |
2 | Correct | 5 ms | 5120 KB | Output is correct |
3 | Correct | 110 ms | 15480 KB | Output is correct |
4 | Correct | 97 ms | 13688 KB | Output is correct |
5 | Correct | 95 ms | 13560 KB | Output is correct |
6 | Correct | 400 ms | 27512 KB | Output is correct |
7 | Correct | 4 ms | 4992 KB | Output is correct |
8 | Correct | 4 ms | 4992 KB | Output is correct |
9 | Correct | 4 ms | 4992 KB | Output is correct |
10 | Correct | 4 ms | 4992 KB | Output is correct |
11 | Correct | 4 ms | 4992 KB | Output is correct |
12 | Correct | 4 ms | 5120 KB | Output is correct |
13 | Correct | 4 ms | 4992 KB | Output is correct |
14 | Correct | 4 ms | 4992 KB | Output is correct |
15 | Correct | 4 ms | 5120 KB | Output is correct |
16 | Correct | 4 ms | 5120 KB | Output is correct |
17 | Correct | 4 ms | 5120 KB | Output is correct |
18 | Correct | 4 ms | 5120 KB | Output is correct |
19 | Correct | 4 ms | 5120 KB | Output is correct |
20 | Correct | 4 ms | 5120 KB | Output is correct |
21 | Correct | 5 ms | 5120 KB | Output is correct |
22 | Correct | 5 ms | 5120 KB | Output is correct |
23 | Correct | 104 ms | 14156 KB | Output is correct |
24 | Correct | 136 ms | 16052 KB | Output is correct |
25 | Correct | 110 ms | 15484 KB | Output is correct |
26 | Correct | 99 ms | 13688 KB | Output is correct |
27 | Correct | 97 ms | 13560 KB | Output is correct |
28 | Correct | 113 ms | 13816 KB | Output is correct |
29 | Correct | 351 ms | 23928 KB | Output is correct |
30 | Correct | 129 ms | 15736 KB | Output is correct |
31 | Correct | 132 ms | 16200 KB | Output is correct |
32 | Correct | 128 ms | 16112 KB | Output is correct |
33 | Correct | 118 ms | 15996 KB | Output is correct |
34 | Correct | 100 ms | 13688 KB | Output is correct |
35 | Correct | 103 ms | 13816 KB | Output is correct |
36 | Correct | 101 ms | 13816 KB | Output is correct |
37 | Correct | 402 ms | 27640 KB | Output is correct |
38 | Correct | 130 ms | 16004 KB | Output is correct |
39 | Correct | 127 ms | 16064 KB | Output is correct |
40 | Correct | 127 ms | 16056 KB | Output is correct |
41 | Correct | 114 ms | 16200 KB | Output is correct |
42 | Correct | 95 ms | 13688 KB | Output is correct |
43 | Correct | 100 ms | 13688 KB | Output is correct |
44 | Correct | 94 ms | 13560 KB | Output is correct |
45 | Correct | 287 ms | 23616 KB | Output is correct |
46 | Correct | 135 ms | 15924 KB | Output is correct |
47 | Correct | 127 ms | 16068 KB | Output is correct |
48 | Correct | 131 ms | 16372 KB | Output is correct |
49 | Correct | 105 ms | 15096 KB | Output is correct |
50 | Correct | 96 ms | 13944 KB | Output is correct |
51 | Correct | 103 ms | 13688 KB | Output is correct |
52 | Correct | 95 ms | 13564 KB | Output is correct |
53 | Correct | 355 ms | 24184 KB | Output is correct |
54 | Correct | 137 ms | 15912 KB | Output is correct |