답안 #196966

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
196966 2020-01-17T22:33:02 Z mahmoudbadawy Pipes (BOI13_pipes) C++17
100 / 100
275 ms 29432 KB
#include <bits/stdc++.h>
#define F first
#define S second

using namespace std;

const int M=5e5+5;
const int N=1e5+5;

int n,m;
int ans[M],arr[N],co[N];
vector<pair<int,int> > adj[N];
int cur;
vector<pair<int,int> > adj2[M];
int vis[N];

int dfs1(int node,int p,int co,int pa)
{
	if(co==cur) return 0;
	for(auto i:adj2[node])
	{
		if(i.F==pa) continue;
		return i.S*p+dfs1(i.F,-1*p,co+1,node);
	}
}

void dfs2(int node,int co,int pa)
{
	if(co==cur) return;
	for(auto i:adj2[node])
	{
		if(i.F==pa) continue;
		ans[i.F]=i.S-ans[node];
		dfs2(i.F,co+1,node);
	}
}

int main()
{
	cin >> n >> m;
	if(m>n)
	{
		cout << 0 << endl;
		return 0;
	}
	for(int i=1;i<=n;i++)
		cin >> arr[i];
	for(int i=0;i<m;i++)
	{
		int x,y;
		cin >> x >> y;
		adj[x].emplace_back(y,i);
		adj[y].emplace_back(x,i);
		co[x]++; co[y]++;
	}
	queue<int> q;
	for(int i=1;i<=n;i++)
	{
		if(adj[i].size()==1) q.push(i);
	}
	while(!q.empty())
	{
		int x=q.front(); q.pop();
		vis[x]=1;
		int to=-1,ed=-1;
		for(int i=0;i<adj[x].size();i++)
		{
			co[adj[x][i].F]--;
			if(!vis[adj[x][i].F])
			{
				to=adj[x][i].F; ed=adj[x][i].S;
			}
			arr[adj[x][i].F]-=arr[x];
		}
		if(to!=-1)
		{
			ans[ed]=2*arr[x];
			if(co[to]==1)
				q.push(to);
		}
	}
	int mn=-1;
	for(int i=1;i<=n;i++)
	{
		if(vis[i]) continue;
		vector<int> te;
		for(auto x:adj[i])
		{
			if(!vis[x.F]) te.push_back(x.S);
		}
		assert(te.size()==2);
		//eq.push_back({{min(te[0],te[1]),max(te[0],te[1])},arr[i]});
		adj2[te[0]].emplace_back(te[1],2*arr[i]);
		adj2[te[1]].emplace_back(te[0],2*arr[i]);
		if(mn==-1) mn=te[0];
		cur++;
	}
	if(cur%2==0&&cur)
	{
		cout << 0 << endl;
		return 0;
	}
	if(cur){
		int a=dfs1(mn,1,0,-1);
		a/=2;
		ans[mn]=a;
		dfs2(mn,0,-1);
	}
	for(int i=0;i<m;i++)
		printf("%d\n",ans[i]);
}

Compilation message

pipes.cpp: In function 'int main()':
pipes.cpp:66:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i=0;i<adj[x].size();i++)
               ~^~~~~~~~~~~~~~
pipes.cpp: In function 'int dfs1(int, int, int, int)':
pipes.cpp:25:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 14712 KB Output is correct
2 Correct 15 ms 14460 KB Output is correct
3 Correct 17 ms 14512 KB Output is correct
4 Correct 211 ms 21820 KB Output is correct
5 Correct 15 ms 14456 KB Output is correct
6 Correct 15 ms 14456 KB Output is correct
7 Correct 15 ms 14456 KB Output is correct
8 Correct 15 ms 14428 KB Output is correct
9 Correct 16 ms 14456 KB Output is correct
10 Correct 17 ms 14456 KB Output is correct
11 Correct 17 ms 14456 KB Output is correct
12 Correct 17 ms 14456 KB Output is correct
13 Correct 169 ms 20472 KB Output is correct
14 Correct 230 ms 21620 KB Output is correct
15 Correct 213 ms 21948 KB Output is correct
16 Correct 189 ms 20772 KB Output is correct
17 Correct 212 ms 21944 KB Output is correct
18 Correct 230 ms 21976 KB Output is correct
19 Correct 217 ms 21240 KB Output is correct
20 Correct 15 ms 14460 KB Output is correct
21 Correct 17 ms 14584 KB Output is correct
22 Correct 214 ms 21848 KB Output is correct
23 Correct 169 ms 20484 KB Output is correct
24 Correct 217 ms 21972 KB Output is correct
25 Correct 196 ms 20728 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 14456 KB Output is correct
2 Correct 21 ms 14584 KB Output is correct
3 Correct 240 ms 22048 KB Output is correct
4 Correct 17 ms 14456 KB Output is correct
5 Correct 15 ms 14456 KB Output is correct
6 Correct 15 ms 14456 KB Output is correct
7 Correct 15 ms 14456 KB Output is correct
8 Correct 15 ms 14456 KB Output is correct
9 Correct 17 ms 14500 KB Output is correct
10 Correct 15 ms 14456 KB Output is correct
11 Correct 15 ms 14456 KB Output is correct
12 Correct 15 ms 14456 KB Output is correct
13 Correct 16 ms 14584 KB Output is correct
14 Correct 16 ms 14456 KB Output is correct
15 Correct 17 ms 14556 KB Output is correct
16 Correct 17 ms 14584 KB Output is correct
17 Correct 17 ms 14584 KB Output is correct
18 Correct 15 ms 14460 KB Output is correct
19 Correct 15 ms 14456 KB Output is correct
20 Correct 15 ms 14328 KB Output is correct
21 Correct 18 ms 14456 KB Output is correct
22 Correct 18 ms 14556 KB Output is correct
23 Correct 201 ms 25060 KB Output is correct
24 Correct 251 ms 26360 KB Output is correct
25 Correct 209 ms 22136 KB Output is correct
26 Correct 15 ms 14456 KB Output is correct
27 Correct 15 ms 14456 KB Output is correct
28 Correct 15 ms 14456 KB Output is correct
29 Correct 16 ms 14428 KB Output is correct
30 Correct 238 ms 25308 KB Output is correct
31 Correct 275 ms 29432 KB Output is correct
32 Correct 226 ms 23300 KB Output is correct
33 Correct 222 ms 22648 KB Output is correct
34 Correct 15 ms 14448 KB Output is correct
35 Correct 15 ms 14456 KB Output is correct
36 Correct 16 ms 14456 KB Output is correct
37 Correct 16 ms 14456 KB Output is correct
38 Correct 247 ms 25592 KB Output is correct
39 Correct 220 ms 22776 KB Output is correct
40 Correct 247 ms 26144 KB Output is correct
41 Correct 236 ms 23544 KB Output is correct
42 Correct 16 ms 14456 KB Output is correct
43 Correct 18 ms 14456 KB Output is correct
44 Correct 19 ms 14456 KB Output is correct
45 Correct 18 ms 14456 KB Output is correct
46 Correct 245 ms 25084 KB Output is correct
47 Correct 242 ms 26340 KB Output is correct
48 Correct 271 ms 29176 KB Output is correct
49 Correct 195 ms 21408 KB Output is correct
50 Correct 15 ms 14584 KB Output is correct
51 Correct 16 ms 14456 KB Output is correct
52 Correct 16 ms 14456 KB Output is correct
53 Correct 15 ms 14456 KB Output is correct
54 Correct 233 ms 24824 KB Output is correct