#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
const int nmax=100005;
vector< pair<int,int> > v[nmax];
pair<int,int> cyc[nmax];
int rep[nmax],wh[nmax],mu[nmax],viz[nmax],ans[nmax],vreau[nmax],am[nmax];
int n,nr,m,i,j,sz,x,y,act,a,b;
void solve(int x)
{
int nod=0;
viz[x]=1;
for(int i=0;i<v[x].size();i++)
if(!viz[v[x][i].first])
{
nod=v[x][i].first;
solve(nod);
ans[v[x][i].second]=vreau[nod]-am[nod];
am[x]+=(vreau[nod]-am[nod]);
}
}
int it=0;
bool gasit;
void dfs(int x)
{
rep[++nr]=x;wh[x]=nr;
for(int i=0;i<v[x].size()&&(!gasit);i++)
{
mu[nr]=v[x][i].second;
if(!wh[v[x][i].first])
{
dfs(v[x][i].first);
}
else
{
if(wh[v[x][i].first]!=nr-1)
{
gasit=1;
for(j=wh[v[x][i].first];j<=nr;j++)
{
cyc[++sz]={rep[j],mu[j]};
}
}
}
}
nr--;
}
void gaseste_ciclu()
{
dfs(1);
for(i=1;i<=sz;i++)
viz[cyc[i].first]=1;
if(!(sz%2))
{
cout<<"0";
return;
}
for(i=1;i<=sz;i++)
{
x=cyc[i].first;
dfs(x);
vreau[x]-=am[x];
}
for(i=1;i<=sz;i++)
{
x=cyc[i].first;
if((i&1)) act+=vreau[x];
else act-=vreau[x];
}
if(act%2)
{
cout<<"0";
return;
}
ans[cyc[sz].second]=act/2;
cyc[0]=cyc[sz];
for(i=1;i<sz;i++)
{
ans[cyc[i].second]=vreau[cyc[i].first]-ans[cyc[i-1].second];
}
for(i=1;i<=m;i++)
cout<<2*ans[i]<<'\n';
}
int main()
{
// freopen("data.in","r",stdin);
ios_base::sync_with_stdio(false);
cin>>n>>m;
if(m>n)
{
cout<<"0";
return 0;
}
for(i=1;i<=n;i++)
{
cin>>vreau[i];
}
for(i=1;i<=m;i++)
{
cin>>a>>b;
v[a].push_back({b,i});
v[b].push_back({a,i});
}
if(m==n-1)
{
solve(1);
if(vreau[1]-am[1])
{
cout<<"0";
return 0;
}
for(i=1;i<=m;i++)
cout<<2*ans[i]<<'\n';
}
else
{
gaseste_ciclu();
}
return 0;
}
Compilation message
pipes.cpp: In function 'void solve(int)':
pipes.cpp:14:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<v[x].size();i++)
~^~~~~~~~~~~~
pipes.cpp: In function 'void dfs(int)':
pipes.cpp:28:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<v[x].size()&&(!gasit);i++)
~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2684 KB |
Output is correct |
2 |
Correct |
4 ms |
2816 KB |
Output is correct |
3 |
Correct |
5 ms |
2892 KB |
Output is correct |
4 |
Correct |
90 ms |
8612 KB |
Output is correct |
5 |
Correct |
4 ms |
8612 KB |
Output is correct |
6 |
Correct |
4 ms |
8612 KB |
Output is correct |
7 |
Correct |
4 ms |
8612 KB |
Output is correct |
8 |
Correct |
5 ms |
8612 KB |
Output is correct |
9 |
Correct |
4 ms |
8612 KB |
Output is correct |
10 |
Correct |
4 ms |
8612 KB |
Output is correct |
11 |
Correct |
5 ms |
8612 KB |
Output is correct |
12 |
Correct |
5 ms |
8612 KB |
Output is correct |
13 |
Correct |
86 ms |
8612 KB |
Output is correct |
14 |
Correct |
82 ms |
8612 KB |
Output is correct |
15 |
Correct |
79 ms |
8840 KB |
Output is correct |
16 |
Correct |
70 ms |
8840 KB |
Output is correct |
17 |
Correct |
96 ms |
8840 KB |
Output is correct |
18 |
Correct |
86 ms |
8972 KB |
Output is correct |
19 |
Correct |
103 ms |
10608 KB |
Output is correct |
20 |
Correct |
4 ms |
10608 KB |
Output is correct |
21 |
Correct |
5 ms |
10608 KB |
Output is correct |
22 |
Correct |
80 ms |
10608 KB |
Output is correct |
23 |
Correct |
66 ms |
10608 KB |
Output is correct |
24 |
Correct |
82 ms |
10608 KB |
Output is correct |
25 |
Correct |
60 ms |
10608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
10608 KB |
Output isn't correct |
2 |
Incorrect |
4 ms |
10608 KB |
Output isn't correct |
3 |
Correct |
76 ms |
10608 KB |
Output is correct |
4 |
Correct |
4 ms |
10608 KB |
Output is correct |
5 |
Correct |
4 ms |
10608 KB |
Output is correct |
6 |
Correct |
4 ms |
10608 KB |
Output is correct |
7 |
Incorrect |
4 ms |
10608 KB |
Output isn't correct |
8 |
Incorrect |
4 ms |
10608 KB |
Output isn't correct |
9 |
Correct |
4 ms |
10608 KB |
Output is correct |
10 |
Correct |
4 ms |
10608 KB |
Output is correct |
11 |
Correct |
4 ms |
10608 KB |
Output is correct |
12 |
Correct |
4 ms |
10608 KB |
Output is correct |
13 |
Correct |
4 ms |
10608 KB |
Output is correct |
14 |
Incorrect |
4 ms |
10608 KB |
Output isn't correct |
15 |
Incorrect |
4 ms |
10608 KB |
Output isn't correct |
16 |
Incorrect |
4 ms |
10608 KB |
Output isn't correct |
17 |
Correct |
5 ms |
10608 KB |
Output is correct |
18 |
Correct |
4 ms |
10608 KB |
Output is correct |
19 |
Correct |
4 ms |
10608 KB |
Output is correct |
20 |
Correct |
4 ms |
10608 KB |
Output is correct |
21 |
Correct |
4 ms |
10608 KB |
Output is correct |
22 |
Incorrect |
4 ms |
10608 KB |
Output isn't correct |
23 |
Incorrect |
97 ms |
10608 KB |
Output isn't correct |
24 |
Incorrect |
78 ms |
10608 KB |
Output isn't correct |
25 |
Correct |
70 ms |
10608 KB |
Output is correct |
26 |
Correct |
5 ms |
10608 KB |
Output is correct |
27 |
Correct |
4 ms |
10608 KB |
Output is correct |
28 |
Correct |
4 ms |
10608 KB |
Output is correct |
29 |
Correct |
4 ms |
10608 KB |
Output is correct |
30 |
Incorrect |
83 ms |
11844 KB |
Output isn't correct |
31 |
Incorrect |
86 ms |
11844 KB |
Output isn't correct |
32 |
Incorrect |
71 ms |
11844 KB |
Output isn't correct |
33 |
Correct |
67 ms |
11844 KB |
Output is correct |
34 |
Correct |
4 ms |
11844 KB |
Output is correct |
35 |
Correct |
4 ms |
11844 KB |
Output is correct |
36 |
Correct |
4 ms |
11844 KB |
Output is correct |
37 |
Correct |
5 ms |
11844 KB |
Output is correct |
38 |
Incorrect |
75 ms |
11844 KB |
Output isn't correct |
39 |
Incorrect |
77 ms |
11844 KB |
Output isn't correct |
40 |
Incorrect |
80 ms |
11844 KB |
Output isn't correct |
41 |
Correct |
83 ms |
11844 KB |
Output is correct |
42 |
Correct |
4 ms |
11844 KB |
Output is correct |
43 |
Correct |
4 ms |
11844 KB |
Output is correct |
44 |
Correct |
4 ms |
11844 KB |
Output is correct |
45 |
Correct |
4 ms |
11844 KB |
Output is correct |
46 |
Incorrect |
94 ms |
11844 KB |
Output isn't correct |
47 |
Incorrect |
86 ms |
11844 KB |
Output isn't correct |
48 |
Incorrect |
109 ms |
11844 KB |
Output isn't correct |
49 |
Correct |
66 ms |
11844 KB |
Output is correct |
50 |
Correct |
4 ms |
11844 KB |
Output is correct |
51 |
Correct |
4 ms |
11844 KB |
Output is correct |
52 |
Correct |
4 ms |
11844 KB |
Output is correct |
53 |
Correct |
4 ms |
11844 KB |
Output is correct |
54 |
Incorrect |
102 ms |
11844 KB |
Output isn't correct |