Submission #127080

# Submission time Handle Problem Language Result Execution time Memory
127080 2019-07-08T21:21:05 Z TadijaSebez Tug of War (BOI15_tug) C++11
100 / 100
2257 ms 5624 KB
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
const int N=60050;
vector<pair<int,int>> E[N];
bool was[N];
int n,k;
int sum[2],deg[N];
int main()
{
	int u,v,w;
	scanf("%i %i",&n,&k);
	for(int i=1;i<=2*n;i++)
	{
		scanf("%i %i %i",&u,&v,&w);
		v+=n;
		E[u].pb({v,w});
		E[v].pb({u,w});
		deg[u]++;
		deg[v]++;
	}
	queue<int> q;
	for(int i=1;i<=2*n;i++) if(deg[i]==1) q.push(i),was[i]=1;
	while(q.size())
	{
		int x=q.front();
		q.pop();
		int b=x>n;
		bool ok=0;
		for(auto e:E[x]) if(!was[e.first])
		{
			ok=1;
			v=e.first;
			w=e.second;
		}
		if(!ok || deg[v]==1) return 0*printf("NO\n");
        sum[b]+=w;
        deg[v]--;
		if(deg[v]==1) was[v]=1,q.push(v);
	}
	for(int i=1;i<=2*n;i++) if(!was[i] && deg[i]!=2) return 0*printf("NO\n");
	vector<int> cyc;
	for(int i=1;i<=2*n;i++) if(!was[i])
	{
		int all=0;
		int par=0;
		for(int x=i,t=1,y;;t=-t)
		{
			int w;
			was[x]=1;
			bool ok=0;
			for(auto e:E[x]) if(deg[e.first]==2 && e.first!=par)
			{
				y=e.first;
				w=e.second;
				ok=1;
			}
			if(!ok)
			{
				vector<int> ws;
				for(auto e:E[i]) if(deg[e.first]==2) ws.pb(e.second);
				all=ws[0]-ws[1];
				break;
			}
			all+=t*w;
			par=x;
			x=y;
			if(x==i) break;
		}
		cyc.pb(all);
	}
	/*set<int> dp;
	dp.insert(sum[1]-sum[0]);
	for(int x:cyc)
	{
		vector<int> ins;
		for(int y:dp)
		{
			ins.pb(y+x);
			ins.pb(y-x);
		}
		dp.clear();
		for(int y:ins) dp.insert(y);
	}
	int ans=1e9;
	for(int y:dp) ans=min(ans,abs(y));*/
	const int lim=N*10;
	bitset<lim> dp;
	dp[lim/2+sum[1]-sum[0]]=1;
	for(int x:cyc)
	{
		x=abs(x);
		dp=(dp>>x)|(dp<<x);
	}
	int ans=1e9;
	for(int i=0;i<N*20;i++) if(dp[i])
	{
		int x=abs(lim/2-i);
		ans=min(ans,x);
	}
	if(ans<=k) printf("YES\n");
	else printf("NO\n");
	return 0;
}

Compilation message

tug.cpp: In function 'int main()':
tug.cpp:12:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%i %i",&n,&k);
  ~~~~~^~~~~~~~~~~~~~~
tug.cpp:15:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%i %i %i",&u,&v,&w);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 2168 KB Output is correct
2 Correct 5 ms 2040 KB Output is correct
3 Correct 6 ms 2040 KB Output is correct
4 Correct 6 ms 2168 KB Output is correct
5 Correct 6 ms 2172 KB Output is correct
6 Correct 6 ms 2168 KB Output is correct
7 Correct 6 ms 2172 KB Output is correct
8 Correct 6 ms 2040 KB Output is correct
9 Correct 6 ms 2040 KB Output is correct
10 Correct 5 ms 2040 KB Output is correct
11 Correct 5 ms 2040 KB Output is correct
12 Correct 6 ms 2040 KB Output is correct
13 Correct 6 ms 2040 KB Output is correct
14 Correct 6 ms 2168 KB Output is correct
15 Correct 6 ms 2168 KB Output is correct
16 Correct 6 ms 2040 KB Output is correct
17 Correct 7 ms 2168 KB Output is correct
18 Correct 7 ms 2172 KB Output is correct
19 Correct 6 ms 2040 KB Output is correct
20 Correct 6 ms 2168 KB Output is correct
21 Correct 3 ms 1784 KB Output is correct
22 Correct 5 ms 2140 KB Output is correct
23 Correct 8 ms 2168 KB Output is correct
24 Correct 6 ms 2168 KB Output is correct
25 Correct 6 ms 2168 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 2168 KB Output is correct
2 Correct 5 ms 2040 KB Output is correct
3 Correct 6 ms 2040 KB Output is correct
4 Correct 6 ms 2168 KB Output is correct
5 Correct 6 ms 2172 KB Output is correct
6 Correct 6 ms 2168 KB Output is correct
7 Correct 6 ms 2172 KB Output is correct
8 Correct 6 ms 2040 KB Output is correct
9 Correct 6 ms 2040 KB Output is correct
10 Correct 5 ms 2040 KB Output is correct
11 Correct 5 ms 2040 KB Output is correct
12 Correct 6 ms 2040 KB Output is correct
13 Correct 6 ms 2040 KB Output is correct
14 Correct 6 ms 2168 KB Output is correct
15 Correct 6 ms 2168 KB Output is correct
16 Correct 6 ms 2040 KB Output is correct
17 Correct 7 ms 2168 KB Output is correct
18 Correct 7 ms 2172 KB Output is correct
19 Correct 6 ms 2040 KB Output is correct
20 Correct 6 ms 2168 KB Output is correct
21 Correct 3 ms 1784 KB Output is correct
22 Correct 5 ms 2140 KB Output is correct
23 Correct 8 ms 2168 KB Output is correct
24 Correct 6 ms 2168 KB Output is correct
25 Correct 6 ms 2168 KB Output is correct
26 Correct 152 ms 2396 KB Output is correct
27 Correct 27 ms 2268 KB Output is correct
28 Correct 150 ms 2268 KB Output is correct
29 Correct 27 ms 2320 KB Output is correct
30 Correct 151 ms 2296 KB Output is correct
31 Correct 27 ms 2296 KB Output is correct
32 Correct 152 ms 2296 KB Output is correct
33 Correct 27 ms 2296 KB Output is correct
34 Correct 18 ms 2296 KB Output is correct
35 Correct 27 ms 2320 KB Output is correct
36 Correct 152 ms 2296 KB Output is correct
37 Correct 27 ms 2296 KB Output is correct
38 Correct 168 ms 2424 KB Output is correct
39 Correct 29 ms 2296 KB Output is correct
40 Correct 153 ms 2300 KB Output is correct
41 Correct 27 ms 2280 KB Output is correct
42 Correct 156 ms 2172 KB Output is correct
43 Correct 27 ms 2168 KB Output is correct
44 Correct 154 ms 2400 KB Output is correct
45 Correct 27 ms 2296 KB Output is correct
46 Correct 154 ms 2172 KB Output is correct
47 Correct 5 ms 1912 KB Output is correct
48 Correct 83 ms 2296 KB Output is correct
49 Correct 81 ms 2296 KB Output is correct
50 Correct 155 ms 2296 KB Output is correct
51 Correct 154 ms 2296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 437 ms 3012 KB Output is correct
2 Correct 14 ms 2680 KB Output is correct
3 Correct 450 ms 3040 KB Output is correct
4 Correct 14 ms 2680 KB Output is correct
5 Correct 434 ms 2936 KB Output is correct
6 Correct 14 ms 2552 KB Output is correct
7 Correct 482 ms 2932 KB Output is correct
8 Correct 15 ms 2552 KB Output is correct
9 Correct 445 ms 3036 KB Output is correct
10 Correct 13 ms 2552 KB Output is correct
11 Correct 447 ms 2936 KB Output is correct
12 Correct 13 ms 2680 KB Output is correct
13 Correct 434 ms 3036 KB Output is correct
14 Correct 438 ms 2936 KB Output is correct
15 Correct 18 ms 2552 KB Output is correct
16 Correct 456 ms 3064 KB Output is correct
17 Correct 14 ms 2552 KB Output is correct
18 Correct 441 ms 3064 KB Output is correct
19 Correct 13 ms 2552 KB Output is correct
20 Correct 438 ms 3036 KB Output is correct
21 Correct 16 ms 2552 KB Output is correct
22 Correct 234 ms 3092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 2168 KB Output is correct
2 Correct 5 ms 2040 KB Output is correct
3 Correct 6 ms 2040 KB Output is correct
4 Correct 6 ms 2168 KB Output is correct
5 Correct 6 ms 2172 KB Output is correct
6 Correct 6 ms 2168 KB Output is correct
7 Correct 6 ms 2172 KB Output is correct
8 Correct 6 ms 2040 KB Output is correct
9 Correct 6 ms 2040 KB Output is correct
10 Correct 5 ms 2040 KB Output is correct
11 Correct 5 ms 2040 KB Output is correct
12 Correct 6 ms 2040 KB Output is correct
13 Correct 6 ms 2040 KB Output is correct
14 Correct 6 ms 2168 KB Output is correct
15 Correct 6 ms 2168 KB Output is correct
16 Correct 6 ms 2040 KB Output is correct
17 Correct 7 ms 2168 KB Output is correct
18 Correct 7 ms 2172 KB Output is correct
19 Correct 6 ms 2040 KB Output is correct
20 Correct 6 ms 2168 KB Output is correct
21 Correct 3 ms 1784 KB Output is correct
22 Correct 5 ms 2140 KB Output is correct
23 Correct 8 ms 2168 KB Output is correct
24 Correct 6 ms 2168 KB Output is correct
25 Correct 6 ms 2168 KB Output is correct
26 Correct 152 ms 2396 KB Output is correct
27 Correct 27 ms 2268 KB Output is correct
28 Correct 150 ms 2268 KB Output is correct
29 Correct 27 ms 2320 KB Output is correct
30 Correct 151 ms 2296 KB Output is correct
31 Correct 27 ms 2296 KB Output is correct
32 Correct 152 ms 2296 KB Output is correct
33 Correct 27 ms 2296 KB Output is correct
34 Correct 18 ms 2296 KB Output is correct
35 Correct 27 ms 2320 KB Output is correct
36 Correct 152 ms 2296 KB Output is correct
37 Correct 27 ms 2296 KB Output is correct
38 Correct 168 ms 2424 KB Output is correct
39 Correct 29 ms 2296 KB Output is correct
40 Correct 153 ms 2300 KB Output is correct
41 Correct 27 ms 2280 KB Output is correct
42 Correct 156 ms 2172 KB Output is correct
43 Correct 27 ms 2168 KB Output is correct
44 Correct 154 ms 2400 KB Output is correct
45 Correct 27 ms 2296 KB Output is correct
46 Correct 154 ms 2172 KB Output is correct
47 Correct 5 ms 1912 KB Output is correct
48 Correct 83 ms 2296 KB Output is correct
49 Correct 81 ms 2296 KB Output is correct
50 Correct 155 ms 2296 KB Output is correct
51 Correct 154 ms 2296 KB Output is correct
52 Correct 437 ms 3012 KB Output is correct
53 Correct 14 ms 2680 KB Output is correct
54 Correct 450 ms 3040 KB Output is correct
55 Correct 14 ms 2680 KB Output is correct
56 Correct 434 ms 2936 KB Output is correct
57 Correct 14 ms 2552 KB Output is correct
58 Correct 482 ms 2932 KB Output is correct
59 Correct 15 ms 2552 KB Output is correct
60 Correct 445 ms 3036 KB Output is correct
61 Correct 13 ms 2552 KB Output is correct
62 Correct 447 ms 2936 KB Output is correct
63 Correct 13 ms 2680 KB Output is correct
64 Correct 434 ms 3036 KB Output is correct
65 Correct 438 ms 2936 KB Output is correct
66 Correct 18 ms 2552 KB Output is correct
67 Correct 456 ms 3064 KB Output is correct
68 Correct 14 ms 2552 KB Output is correct
69 Correct 441 ms 3064 KB Output is correct
70 Correct 13 ms 2552 KB Output is correct
71 Correct 438 ms 3036 KB Output is correct
72 Correct 16 ms 2552 KB Output is correct
73 Correct 234 ms 3092 KB Output is correct
74 Correct 2213 ms 4544 KB Output is correct
75 Correct 124 ms 5112 KB Output is correct
76 Correct 2195 ms 5372 KB Output is correct
77 Correct 123 ms 5112 KB Output is correct
78 Correct 2225 ms 5368 KB Output is correct
79 Correct 2220 ms 5368 KB Output is correct
80 Correct 133 ms 5240 KB Output is correct
81 Correct 138 ms 5240 KB Output is correct
82 Correct 2256 ms 5340 KB Output is correct
83 Correct 2215 ms 5368 KB Output is correct
84 Correct 126 ms 5112 KB Output is correct
85 Correct 2251 ms 5368 KB Output is correct
86 Correct 121 ms 5112 KB Output is correct
87 Correct 2203 ms 5368 KB Output is correct
88 Correct 122 ms 5240 KB Output is correct
89 Correct 2201 ms 5368 KB Output is correct
90 Correct 122 ms 5112 KB Output is correct
91 Correct 2201 ms 5368 KB Output is correct
92 Correct 131 ms 5240 KB Output is correct
93 Correct 2204 ms 5260 KB Output is correct
94 Correct 37 ms 4984 KB Output is correct
95 Correct 1123 ms 5496 KB Output is correct
96 Correct 1125 ms 5624 KB Output is correct
97 Correct 2257 ms 5368 KB Output is correct
98 Correct 2251 ms 5348 KB Output is correct