#include<bits/stdc++.h>
using namespace std;
#define int long long
#define maxn 100050
#define f first
#define s second
vector<int> sum(maxn,0);
vector<int> currsum(maxn,0);
bool visited[2][maxn];
vector<int> ans(maxn);
vector<int> degree(maxn,0);
vector<vector<pair<int,int> > > adj(maxn);
void dfs(int node,int parity){
visited[parity][node] = 1;
for(auto k:adj[node]){
if(visited[!parity][k.f]==0){
dfs(k.f,!parity);
}
}
}
void dfs2(int node,vector<int> &curr){
curr.push_back(node);
degree[node]--;
for(auto k:adj[node]){
if(degree[k.f]==2){
dfs2(k.f,curr);
}
}
}
int32_t main(){
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int no_of_vertex,no_of_edge;
int input1,input2;
cin >> no_of_vertex >> no_of_edge;
if(no_of_vertex<no_of_edge){
cout << 0;
return 0;
}
for(int i=1;i<=no_of_vertex;i++){
cin >> sum[i];
}
for(int i=0;i<no_of_edge;i++){
cin >> input1 >> input2;
degree[input1]++;
degree[input2]++;
adj[input1].push_back({input2,i});
adj[input2].push_back({input1,i});
}
dfs(1,0);
for(int i=1;i<=no_of_vertex;i++){
if(visited[0][i]==1&&visited[1][i]==1){
}
else if(no_of_edge!=no_of_vertex-1){
cout << 0;
return 0;
}
}
deque<int> q1;
for(int i=1;i<=no_of_vertex;i++){
if(degree[i]==1){
q1.push_back(i);
}
}
while(q1.size()!=0){
int a = q1.front();
q1.pop_front();
degree[a]--;
for(auto k:adj[a]){
if(degree[k.f]==0){
continue;
}
degree[k.f]--;
currsum[k.f] += (sum[a]-currsum[a]);
ans[k.s] = (sum[a]-currsum[a]);
if(degree[k.f]==1){
q1.push_back(k.f);
}
}
}
vector<int> curr;
for(int i=1;i<=no_of_vertex;i++){
if(degree[i]==2){
dfs2(i,curr);
break;
}
}
if(curr.size()==0){
for(int i=0;i<no_of_edge;i++){
cout << ans[i]*2 << "\n";
}
return 0;
}
int temp = 0;
for(int i=1;i<curr.size();i++){
if(i%2==1){
temp += sum[curr[i]]-currsum[curr[i]];
}
else{
temp -= sum[curr[i]]-currsum[curr[i]];
}
}
int rn = ((sum[curr[0]]-currsum[curr[0]])-temp)/2;
currsum[curr[0]] += rn;
currsum[curr[curr.size()-1]]+=rn;
for(auto k:adj[curr[0]]){
if(k.f==curr[curr.size()-1]){
ans[k.s] = rn;
}
}
for(int i=0;i<curr.size()-1;i++){
int nextval = sum[curr[i]]-currsum[curr[i]];
for(auto k:adj[curr[i]]){
if(k.f==curr[i+1]){
ans[k.s] = nextval;
currsum[curr[i+1]] += nextval;
}
}
}
for(int i=0;i<no_of_edge;i++){
//assert(ans[i]!=0);
cout << ans[i]*2 << "\n";
}
}
Compilation message
pipes.cpp: In function 'int32_t main()':
pipes.cpp:95:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=1;i<curr.size();i++){
~^~~~~~~~~~~~
pipes.cpp:111:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<curr.size()-1;i++){
~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
5888 KB |
Output is correct |
2 |
Correct |
3 ms |
5888 KB |
Output is correct |
3 |
Correct |
5 ms |
5888 KB |
Output is correct |
4 |
Correct |
83 ms |
13376 KB |
Output is correct |
5 |
Correct |
5 ms |
5888 KB |
Output is correct |
6 |
Correct |
3 ms |
5888 KB |
Output is correct |
7 |
Correct |
3 ms |
5888 KB |
Output is correct |
8 |
Correct |
5 ms |
5888 KB |
Output is correct |
9 |
Correct |
4 ms |
5888 KB |
Output is correct |
10 |
Correct |
4 ms |
5888 KB |
Output is correct |
11 |
Correct |
4 ms |
5888 KB |
Output is correct |
12 |
Correct |
4 ms |
5888 KB |
Output is correct |
13 |
Correct |
64 ms |
12024 KB |
Output is correct |
14 |
Correct |
91 ms |
13124 KB |
Output is correct |
15 |
Correct |
87 ms |
13496 KB |
Output is correct |
16 |
Correct |
73 ms |
12408 KB |
Output is correct |
17 |
Correct |
97 ms |
13376 KB |
Output is correct |
18 |
Correct |
96 ms |
13372 KB |
Output is correct |
19 |
Correct |
98 ms |
15608 KB |
Output is correct |
20 |
Correct |
4 ms |
5888 KB |
Output is correct |
21 |
Correct |
4 ms |
5888 KB |
Output is correct |
22 |
Correct |
84 ms |
13372 KB |
Output is correct |
23 |
Correct |
62 ms |
12024 KB |
Output is correct |
24 |
Correct |
91 ms |
13368 KB |
Output is correct |
25 |
Correct |
73 ms |
12280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5888 KB |
Output is correct |
2 |
Correct |
4 ms |
6016 KB |
Output is correct |
3 |
Correct |
73 ms |
14452 KB |
Output is correct |
4 |
Correct |
3 ms |
5888 KB |
Output is correct |
5 |
Correct |
3 ms |
5888 KB |
Output is correct |
6 |
Correct |
3 ms |
5888 KB |
Output is correct |
7 |
Correct |
4 ms |
5888 KB |
Output is correct |
8 |
Correct |
3 ms |
5888 KB |
Output is correct |
9 |
Correct |
4 ms |
5888 KB |
Output is correct |
10 |
Correct |
3 ms |
5888 KB |
Output is correct |
11 |
Correct |
5 ms |
5888 KB |
Output is correct |
12 |
Correct |
3 ms |
5888 KB |
Output is correct |
13 |
Correct |
3 ms |
5888 KB |
Output is correct |
14 |
Correct |
3 ms |
5888 KB |
Output is correct |
15 |
Correct |
4 ms |
6016 KB |
Output is correct |
16 |
Correct |
4 ms |
5888 KB |
Output is correct |
17 |
Correct |
4 ms |
5888 KB |
Output is correct |
18 |
Correct |
3 ms |
5888 KB |
Output is correct |
19 |
Correct |
3 ms |
5888 KB |
Output is correct |
20 |
Correct |
3 ms |
5888 KB |
Output is correct |
21 |
Correct |
3 ms |
5888 KB |
Output is correct |
22 |
Correct |
5 ms |
6016 KB |
Output is correct |
23 |
Correct |
87 ms |
17392 KB |
Output is correct |
24 |
Correct |
138 ms |
18800 KB |
Output is correct |
25 |
Correct |
64 ms |
14456 KB |
Output is correct |
26 |
Correct |
5 ms |
5888 KB |
Output is correct |
27 |
Correct |
5 ms |
5888 KB |
Output is correct |
28 |
Correct |
4 ms |
5888 KB |
Output is correct |
29 |
Correct |
5 ms |
5888 KB |
Output is correct |
30 |
Correct |
139 ms |
19756 KB |
Output is correct |
31 |
Correct |
158 ms |
22260 KB |
Output is correct |
32 |
Correct |
119 ms |
15224 KB |
Output is correct |
33 |
Correct |
87 ms |
15352 KB |
Output is correct |
34 |
Correct |
4 ms |
5888 KB |
Output is correct |
35 |
Correct |
3 ms |
5888 KB |
Output is correct |
36 |
Correct |
4 ms |
5888 KB |
Output is correct |
37 |
Correct |
3 ms |
5888 KB |
Output is correct |
38 |
Correct |
133 ms |
19408 KB |
Output is correct |
39 |
Correct |
106 ms |
14448 KB |
Output is correct |
40 |
Correct |
136 ms |
18416 KB |
Output is correct |
41 |
Correct |
79 ms |
16888 KB |
Output is correct |
42 |
Correct |
4 ms |
5888 KB |
Output is correct |
43 |
Correct |
5 ms |
5888 KB |
Output is correct |
44 |
Correct |
4 ms |
5888 KB |
Output is correct |
45 |
Correct |
4 ms |
5888 KB |
Output is correct |
46 |
Correct |
143 ms |
20044 KB |
Output is correct |
47 |
Correct |
133 ms |
18448 KB |
Output is correct |
48 |
Correct |
145 ms |
21872 KB |
Output is correct |
49 |
Correct |
63 ms |
13048 KB |
Output is correct |
50 |
Correct |
3 ms |
5888 KB |
Output is correct |
51 |
Correct |
3 ms |
5888 KB |
Output is correct |
52 |
Correct |
3 ms |
5888 KB |
Output is correct |
53 |
Correct |
3 ms |
5888 KB |
Output is correct |
54 |
Correct |
111 ms |
18740 KB |
Output is correct |