답안 #107059

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
107059 2019-04-21T15:50:02 Z Pajaraja Pipes (BOI13_pipes) C++17
74.0741 / 100
389 ms 24436 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],parc[MAXN];
long long t[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;}
		long long tz=0;
		for(int i=0;i<c.size();i++) tz+=t[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+1]-parc[i]);
		mp[make_pair(c[0],c[c.size()-1])]=mp[make_pair(c[c.size()-1],c[0])]=2*(tz-parc[c.size()-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:49:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i=0;i<c.size();i++) tz+=t[i];
               ~^~~~~~~~~
pipes.cpp:51: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:53: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:54: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+1]-parc[i]);
               ~^~~~~~~~~~~
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 5 ms 2688 KB Output is correct
2 Correct 4 ms 2688 KB Output is correct
3 Correct 5 ms 2944 KB Output is correct
4 Correct 341 ms 20920 KB Output is correct
5 Correct 5 ms 2688 KB Output is correct
6 Correct 4 ms 2688 KB Output is correct
7 Correct 4 ms 2816 KB Output is correct
8 Correct 4 ms 2688 KB Output is correct
9 Correct 5 ms 2816 KB Output is correct
10 Correct 5 ms 2944 KB Output is correct
11 Correct 5 ms 2816 KB Output is correct
12 Correct 5 ms 2816 KB Output is correct
13 Correct 285 ms 17272 KB Output is correct
14 Correct 341 ms 19960 KB Output is correct
15 Correct 377 ms 21112 KB Output is correct
16 Correct 315 ms 18296 KB Output is correct
17 Correct 389 ms 21044 KB Output is correct
18 Correct 348 ms 20984 KB Output is correct
19 Correct 287 ms 20856 KB Output is correct
20 Correct 6 ms 2688 KB Output is correct
21 Correct 5 ms 2816 KB Output is correct
22 Correct 305 ms 20924 KB Output is correct
23 Correct 229 ms 17272 KB Output is correct
24 Correct 318 ms 21112 KB Output is correct
25 Correct 239 ms 17912 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 2688 KB Output isn't correct
2 Incorrect 5 ms 2944 KB Output isn't correct
3 Correct 181 ms 15532 KB Output is correct
4 Correct 120 ms 10616 KB Output is correct
5 Correct 254 ms 16652 KB Output is correct
6 Correct 333 ms 15760 KB Output is correct
7 Incorrect 4 ms 2688 KB Output isn't correct
8 Incorrect 4 ms 2688 KB Output isn't correct
9 Correct 5 ms 2688 KB Output is correct
10 Correct 4 ms 2688 KB Output is correct
11 Correct 4 ms 2688 KB Output is correct
12 Correct 5 ms 2688 KB Output is correct
13 Correct 4 ms 2688 KB Output is correct
14 Incorrect 4 ms 2688 KB Output isn't correct
15 Incorrect 5 ms 2944 KB Output isn't correct
16 Incorrect 5 ms 2816 KB Output isn't correct
17 Correct 5 ms 2816 KB Output is correct
18 Correct 4 ms 2816 KB Output is correct
19 Correct 6 ms 2752 KB Output is correct
20 Correct 5 ms 2816 KB Output is correct
21 Correct 7 ms 2864 KB Output is correct
22 Incorrect 7 ms 2816 KB Output isn't correct
23 Incorrect 260 ms 19828 KB Output isn't correct
24 Incorrect 359 ms 23156 KB Output isn't correct
25 Correct 189 ms 15604 KB Output is correct
26 Correct 148 ms 11636 KB Output is correct
27 Correct 89 ms 9080 KB Output is correct
28 Correct 187 ms 14900 KB Output is correct
29 Correct 293 ms 13688 KB Output is correct
30 Incorrect 355 ms 22328 KB Output isn't correct
31 Incorrect 363 ms 24436 KB Output isn't correct
32 Incorrect 297 ms 21624 KB Output isn't correct
33 Correct 146 ms 15608 KB Output is correct
34 Correct 95 ms 10360 KB Output is correct
35 Correct 95 ms 10728 KB Output is correct
36 Correct 173 ms 15592 KB Output is correct
37 Correct 327 ms 15716 KB Output is correct
38 Incorrect 348 ms 22956 KB Output isn't correct
39 Incorrect 388 ms 21288 KB Output isn't correct
40 Incorrect 303 ms 22900 KB Output isn't correct
41 Correct 131 ms 12660 KB Output is correct
42 Correct 85 ms 8184 KB Output is correct
43 Correct 67 ms 8272 KB Output is correct
44 Correct 238 ms 16704 KB Output is correct
45 Correct 183 ms 13432 KB Output is correct
46 Incorrect 249 ms 22616 KB Output isn't correct
47 Incorrect 282 ms 23032 KB Output isn't correct
48 Incorrect 265 ms 24300 KB Output isn't correct
49 Correct 226 ms 18296 KB Output is correct
50 Correct 167 ms 12408 KB Output is correct
51 Correct 142 ms 14332 KB Output is correct
52 Correct 88 ms 10488 KB Output is correct
53 Correct 213 ms 13692 KB Output is correct
54 Incorrect 280 ms 22260 KB Output isn't correct