답안 #107064

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
107064 2019-04-21T16:13:35 Z Pajaraja Pipes (BOI13_pipes) C++17
100 / 100
435 ms 24560 KB
#include <bits/stdc++.h>
#define MAXN 100007
#define MAXM 500007
using namespace std;
map<pair<int,int>,long long> mp;
vector<int> g[MAXN],c;
int gr[MAXM][2],n,m,deg[MAXN];
long long t[MAXN],parc[MAXN];
bool vi[MAXN];
void dfs(int s)
{
	c.push_back(s);
	vi[s]=true;
	for(int i=0;i<g[s].size();i++) if(!vi[g[s][i]]) dfs(g[s][i]);
}
int main()
{
	int n,m;
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++) scanf("%lld",&t[i]);
	for(int i=0;i<m;i++) 
	{
		scanf("%d%d",&gr[i][0],&gr[i][1]);
		g[gr[i][0]].push_back(gr[i][1]);
		g[gr[i][1]].push_back(gr[i][0]);
	}
	queue<int> q;
	for(int i=1;i<=n;i++) deg[i]=g[i].size();
	for(int i=1;i<=n;i++) if(deg[i]==1) q.push(i);
	while(!q.empty())
	{
		int u=q.front(); q.pop();
		vi[u]=true;
		for(int i=0;i<g[u].size();i++) if(!vi[g[u][i]]) 
		{	
			deg[g[u][i]]--;
			t[g[u][i]]-=t[u];
			mp[make_pair(u,g[u][i])]=mp[make_pair(g[u][i],u)]=2*t[u];
			if(deg[g[u][i]]==1) q.push(g[u][i]);
		}
	}
	for(int i=1;i<=n;i++) if(deg[i]>2) {printf("0"); return 0;}
	for(int i=1;i<=n;i++) if(!vi[i])
	{
		c.clear();
		dfs(i);
		if(c.size()%2==0) {printf("0"); return 0;}
		if(c.size()==1) continue;
		long long tz=0;
		for(int i=0;i<c.size();i++) tz+=t[c[i]];
		tz/=2;
		for(int i=2;i<c.size();i+=2) parc[i]=parc[i-2]+t[c[i-2]];
		parc[1]=parc[c.size()-1]+t[c[c.size()-1]]-tz;
		for(int i=3;i<=c.size();i+=2) parc[i]=parc[i-2]+t[c[i-2]];
		for(int i=0;i<c.size()-1;i++) mp[make_pair(c[i],c[i+1])]=mp[make_pair(c[i+1],c[i])]=2*(parc[i+2]-parc[i+1]);
		mp[make_pair(c[c.size()-1],c[c.size()-2])]=mp[make_pair(c[c.size()-2],c[c.size()-1])]=2*(tz-parc[c.size()-1]);
		mp[make_pair(c[c.size()-1],c[0])]=mp[make_pair(c[0],c[c.size()-1])]=2*(parc[1]);
	}
	for(int i=0;i<m;i++) printf("%lld\n",mp[make_pair(gr[i][0],gr[i][1])]);
}

Compilation message

pipes.cpp: In function 'void dfs(int)':
pipes.cpp:14:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<g[s].size();i++) if(!vi[g[s][i]]) dfs(g[s][i]);
              ~^~~~~~~~~~~~
pipes.cpp: In function 'int main()':
pipes.cpp:34:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i=0;i<g[u].size();i++) if(!vi[g[u][i]]) 
               ~^~~~~~~~~~~~
pipes.cpp:50:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i=0;i<c.size();i++) tz+=t[c[i]];
               ~^~~~~~~~~
pipes.cpp:52:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i=2;i<c.size();i+=2) parc[i]=parc[i-2]+t[c[i-2]];
               ~^~~~~~~~~
pipes.cpp:54:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i=3;i<=c.size();i+=2) parc[i]=parc[i-2]+t[c[i-2]];
               ~^~~~~~~~~~
pipes.cpp:55:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i=0;i<c.size()-1;i++) mp[make_pair(c[i],c[i+1])]=mp[make_pair(c[i+1],c[i])]=2*(parc[i+2]-parc[i+1]);
               ~^~~~~~~~~~~
pipes.cpp:19:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d",&n,&m);
  ~~~~~^~~~~~~~~~~~~~
pipes.cpp:20:29: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for(int i=1;i<=n;i++) scanf("%lld",&t[i]);
                        ~~~~~^~~~~~~~~~~~~~
pipes.cpp:23:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d",&gr[i][0],&gr[i][1]);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2688 KB Output is correct
2 Correct 4 ms 2688 KB Output is correct
3 Correct 6 ms 2944 KB Output is correct
4 Correct 281 ms 20992 KB Output is correct
5 Correct 4 ms 2688 KB Output is correct
6 Correct 4 ms 2688 KB Output is correct
7 Correct 4 ms 2688 KB Output is correct
8 Correct 4 ms 2688 KB Output is correct
9 Correct 5 ms 2816 KB Output is correct
10 Correct 6 ms 2944 KB Output is correct
11 Correct 8 ms 3072 KB Output is correct
12 Correct 6 ms 2816 KB Output is correct
13 Correct 289 ms 17248 KB Output is correct
14 Correct 382 ms 20060 KB Output is correct
15 Correct 435 ms 20992 KB Output is correct
16 Correct 276 ms 18168 KB Output is correct
17 Correct 364 ms 20944 KB Output is correct
18 Correct 379 ms 21112 KB Output is correct
19 Correct 332 ms 20932 KB Output is correct
20 Correct 6 ms 2688 KB Output is correct
21 Correct 7 ms 2944 KB Output is correct
22 Correct 385 ms 20948 KB Output is correct
23 Correct 290 ms 17144 KB Output is correct
24 Correct 410 ms 20984 KB Output is correct
25 Correct 307 ms 17900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 2688 KB Output is correct
2 Correct 6 ms 2944 KB Output is correct
3 Correct 192 ms 15604 KB Output is correct
4 Correct 117 ms 10744 KB Output is correct
5 Correct 265 ms 16776 KB Output is correct
6 Correct 390 ms 15860 KB Output is correct
7 Correct 5 ms 2688 KB Output is correct
8 Correct 7 ms 2688 KB Output is correct
9 Correct 5 ms 2688 KB Output is correct
10 Correct 5 ms 2688 KB Output is correct
11 Correct 4 ms 2688 KB Output is correct
12 Correct 4 ms 2688 KB Output is correct
13 Correct 4 ms 2688 KB Output is correct
14 Correct 5 ms 2688 KB Output is correct
15 Correct 7 ms 2944 KB Output is correct
16 Correct 5 ms 2816 KB Output is correct
17 Correct 5 ms 2788 KB Output is correct
18 Correct 6 ms 2816 KB Output is correct
19 Correct 5 ms 2816 KB Output is correct
20 Correct 6 ms 2688 KB Output is correct
21 Correct 4 ms 2816 KB Output is correct
22 Correct 6 ms 2944 KB Output is correct
23 Correct 278 ms 19836 KB Output is correct
24 Correct 320 ms 23156 KB Output is correct
25 Correct 189 ms 15604 KB Output is correct
26 Correct 133 ms 11640 KB Output is correct
27 Correct 92 ms 8952 KB Output is correct
28 Correct 195 ms 14844 KB Output is correct
29 Correct 223 ms 13840 KB Output is correct
30 Correct 290 ms 22520 KB Output is correct
31 Correct 272 ms 24560 KB Output is correct
32 Correct 353 ms 21624 KB Output is correct
33 Correct 170 ms 15608 KB Output is correct
34 Correct 103 ms 10360 KB Output is correct
35 Correct 114 ms 10744 KB Output is correct
36 Correct 224 ms 15632 KB Output is correct
37 Correct 350 ms 15648 KB Output is correct
38 Correct 309 ms 23032 KB Output is correct
39 Correct 341 ms 21244 KB Output is correct
40 Correct 289 ms 23028 KB Output is correct
41 Correct 127 ms 12664 KB Output is correct
42 Correct 96 ms 8288 KB Output is correct
43 Correct 76 ms 8284 KB Output is correct
44 Correct 197 ms 16632 KB Output is correct
45 Correct 246 ms 13396 KB Output is correct
46 Correct 324 ms 22776 KB Output is correct
47 Correct 312 ms 23032 KB Output is correct
48 Correct 290 ms 24556 KB Output is correct
49 Correct 260 ms 18296 KB Output is correct
50 Correct 139 ms 12408 KB Output is correct
51 Correct 204 ms 14328 KB Output is correct
52 Correct 119 ms 10616 KB Output is correct
53 Correct 278 ms 13816 KB Output is correct
54 Correct 308 ms 22360 KB Output is correct