답안 #253036

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
253036 2020-07-26T17:28:33 Z Blagojce Tug of War (BOI15_tug) C++11
100 / 100
2309 ms 6772 KB
#include <bits/stdc++.h> 
#define fr(i, n, m) for(int i = (n); i < (m); i ++)
#define pb push_back
#define st first
#define nd second
#define pq priority_queue
#define all(x) begin(x), end(x)
#include <time.h>
#include <cmath>

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;

const int i_inf = 1e9;
const ll inf = 1e18;
const ll mod = 1000000007;
const ld eps = 1e-13;
const ld pi  = 3.14159265359;
 
mt19937 _rand(time(NULL));
clock_t timer = clock();
const int mxn = 60000;

int n, k;
int l[mxn], r[mxn], s[mxn];

vector<int> g[mxn];

int deg[mxn];

int used[mxn];
int vis[mxn];


int nxt(int u, int ed){
	return l[ed]^r[ed]^u;
}
void build(){
	fr(i, 0, n){
		l[i] = l[i]-1;
		r[i] = r[i]-1+n/2;
	}
	fr(i, 0, n){
		g[l[i]].pb(i);
		g[r[i]].pb(i);
		
		++deg[l[i]];
		++deg[r[i]];
	}
}
void remove(int ed){
	used[ed] = 3;
	--deg[l[ed]], --deg[r[ed]];	
}  
void update(int &sa, int &sb, int u, int ed){
	if(u == l[ed]) sa += s[ed];
	else sb += s[ed];
}

int find_one(int u){
	for(auto e : g[u]){
		if(used[e] == 3) continue;
		return e;
	}
	return -1;
}
int suma, sumb;
void pruning(){
	queue<int> Q;
	fr(i, 0, n) if(deg[i] == 1) Q.push(i);
	while(!Q.empty()){
		int u = Q.front();
		Q.pop();
		int ed = find_one(u);
		if(ed == -1) continue;
		
		remove(ed);
		update(suma, sumb, u, ed);
		if(deg[nxt(u, ed)] == 1) Q.push(nxt(u,ed));
	}
}
void dfs(int u, int flag, int &sa, int &sb){
	for(auto e : g[u]){
		if(used[e] >= flag) continue;
		if(vis[nxt(u, e)] == flag){
			printf("NO \n");
			exit(0);
		}
		used[e] = flag, vis[nxt(u, e)] = flag;
		update(sa, sb, nxt(u, e), e);
		dfs(nxt(u, e), flag, sa, sb);
	}
}
pii direct_edge(int ed){
	int sa1 = s[ed], sb1 = 0;
	used[ed] = 1, vis[l[ed]] = 1;
	dfs(l[ed], 1, sa1, sb1);

	int sa2 = 0, sb2 = s[ed];
	used[ed] = 2, vis[r[ed]] = 2;
	dfs(r[ed], 2, sa2, sb2);
		
	return {sa1-sb1, sa2-sb2};
}

bitset<15*mxn> dp;

void solve(){
	scanf("%i %i", &n, &k);
	n <<= 1;
	bool sub3 = true;
	fr(i, 0, n){
		scanf("%i %i %i", &l[i], &r[i], &s[i]); 
		if(s[i] != 1) sub3 = false;
	}
	build();
	pruning();
	
	vector<pii> v;
	
	fr(i, 0, n){
		if(used[i] >= 2) continue;	
		v.pb(direct_edge(i));
	}
	if(sub3){
		printf("YES \n");
		return;
	}
	
	int m = v.size();
	
	dp[20*(n>>1)+suma-sumb] = 1;
	
	fr(i, 0, m){
		dp = (v[i].st > 0 ? dp << v[i].st : dp >> -v[i].st)|(v[i].nd > 0 ? dp << v[i].nd : dp >> -v[i].nd);
	}
	
	fr(i, 0, k+1){
		if(dp[20*(n>>1)+i]||dp[20*(n>>1)-i]){
			printf("YES \n");
			return;
		}
	}
	
	printf("NO \n");
}

 
int main(){
	solve();
}

Compilation message

tug.cpp: In function 'void solve()':
tug.cpp:111: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:115:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%i %i %i", &l[i], &r[i], &s[i]); 
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2304 KB Output is correct
2 Correct 4 ms 2304 KB Output is correct
3 Correct 2 ms 2304 KB Output is correct
4 Correct 4 ms 2304 KB Output is correct
5 Correct 2 ms 2304 KB Output is correct
6 Correct 3 ms 2304 KB Output is correct
7 Correct 2 ms 2304 KB Output is correct
8 Correct 2 ms 2304 KB Output is correct
9 Correct 2 ms 2304 KB Output is correct
10 Correct 2 ms 2304 KB Output is correct
11 Correct 2 ms 2304 KB Output is correct
12 Correct 2 ms 2304 KB Output is correct
13 Correct 2 ms 2304 KB Output is correct
14 Correct 2 ms 2304 KB Output is correct
15 Correct 2 ms 2304 KB Output is correct
16 Correct 2 ms 2304 KB Output is correct
17 Correct 3 ms 2304 KB Output is correct
18 Correct 3 ms 2304 KB Output is correct
19 Correct 2 ms 2304 KB Output is correct
20 Correct 2 ms 2304 KB Output is correct
21 Correct 1 ms 1792 KB Output is correct
22 Correct 2 ms 2304 KB Output is correct
23 Correct 2 ms 2304 KB Output is correct
24 Correct 2 ms 2304 KB Output is correct
25 Correct 2 ms 2304 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2304 KB Output is correct
2 Correct 4 ms 2304 KB Output is correct
3 Correct 2 ms 2304 KB Output is correct
4 Correct 4 ms 2304 KB Output is correct
5 Correct 2 ms 2304 KB Output is correct
6 Correct 3 ms 2304 KB Output is correct
7 Correct 2 ms 2304 KB Output is correct
8 Correct 2 ms 2304 KB Output is correct
9 Correct 2 ms 2304 KB Output is correct
10 Correct 2 ms 2304 KB Output is correct
11 Correct 2 ms 2304 KB Output is correct
12 Correct 2 ms 2304 KB Output is correct
13 Correct 2 ms 2304 KB Output is correct
14 Correct 2 ms 2304 KB Output is correct
15 Correct 2 ms 2304 KB Output is correct
16 Correct 2 ms 2304 KB Output is correct
17 Correct 3 ms 2304 KB Output is correct
18 Correct 3 ms 2304 KB Output is correct
19 Correct 2 ms 2304 KB Output is correct
20 Correct 2 ms 2304 KB Output is correct
21 Correct 1 ms 1792 KB Output is correct
22 Correct 2 ms 2304 KB Output is correct
23 Correct 2 ms 2304 KB Output is correct
24 Correct 2 ms 2304 KB Output is correct
25 Correct 2 ms 2304 KB Output is correct
26 Correct 141 ms 2560 KB Output is correct
27 Correct 23 ms 2560 KB Output is correct
28 Correct 142 ms 2600 KB Output is correct
29 Correct 23 ms 2560 KB Output is correct
30 Correct 143 ms 2596 KB Output is correct
31 Correct 23 ms 2560 KB Output is correct
32 Correct 146 ms 2560 KB Output is correct
33 Correct 24 ms 2560 KB Output is correct
34 Correct 13 ms 2560 KB Output is correct
35 Correct 23 ms 2560 KB Output is correct
36 Correct 145 ms 2560 KB Output is correct
37 Correct 23 ms 2560 KB Output is correct
38 Correct 141 ms 2560 KB Output is correct
39 Correct 22 ms 2560 KB Output is correct
40 Correct 145 ms 2596 KB Output is correct
41 Correct 32 ms 2560 KB Output is correct
42 Correct 147 ms 2560 KB Output is correct
43 Correct 26 ms 2560 KB Output is correct
44 Correct 157 ms 2680 KB Output is correct
45 Correct 23 ms 2560 KB Output is correct
46 Correct 149 ms 2592 KB Output is correct
47 Correct 3 ms 1920 KB Output is correct
48 Correct 86 ms 2560 KB Output is correct
49 Correct 83 ms 2560 KB Output is correct
50 Correct 157 ms 2560 KB Output is correct
51 Correct 172 ms 2560 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 3072 KB Output is correct
2 Correct 11 ms 2816 KB Output is correct
3 Correct 13 ms 3072 KB Output is correct
4 Correct 11 ms 2816 KB Output is correct
5 Correct 12 ms 3048 KB Output is correct
6 Correct 11 ms 2816 KB Output is correct
7 Correct 12 ms 3072 KB Output is correct
8 Correct 11 ms 2816 KB Output is correct
9 Correct 12 ms 3072 KB Output is correct
10 Correct 11 ms 2816 KB Output is correct
11 Correct 12 ms 3072 KB Output is correct
12 Correct 11 ms 2816 KB Output is correct
13 Correct 16 ms 3028 KB Output is correct
14 Correct 11 ms 3072 KB Output is correct
15 Correct 11 ms 2816 KB Output is correct
16 Correct 12 ms 3072 KB Output is correct
17 Correct 11 ms 2816 KB Output is correct
18 Correct 12 ms 3072 KB Output is correct
19 Correct 16 ms 2816 KB Output is correct
20 Correct 12 ms 3072 KB Output is correct
21 Correct 13 ms 2816 KB Output is correct
22 Correct 17 ms 2944 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2304 KB Output is correct
2 Correct 4 ms 2304 KB Output is correct
3 Correct 2 ms 2304 KB Output is correct
4 Correct 4 ms 2304 KB Output is correct
5 Correct 2 ms 2304 KB Output is correct
6 Correct 3 ms 2304 KB Output is correct
7 Correct 2 ms 2304 KB Output is correct
8 Correct 2 ms 2304 KB Output is correct
9 Correct 2 ms 2304 KB Output is correct
10 Correct 2 ms 2304 KB Output is correct
11 Correct 2 ms 2304 KB Output is correct
12 Correct 2 ms 2304 KB Output is correct
13 Correct 2 ms 2304 KB Output is correct
14 Correct 2 ms 2304 KB Output is correct
15 Correct 2 ms 2304 KB Output is correct
16 Correct 2 ms 2304 KB Output is correct
17 Correct 3 ms 2304 KB Output is correct
18 Correct 3 ms 2304 KB Output is correct
19 Correct 2 ms 2304 KB Output is correct
20 Correct 2 ms 2304 KB Output is correct
21 Correct 1 ms 1792 KB Output is correct
22 Correct 2 ms 2304 KB Output is correct
23 Correct 2 ms 2304 KB Output is correct
24 Correct 2 ms 2304 KB Output is correct
25 Correct 2 ms 2304 KB Output is correct
26 Correct 141 ms 2560 KB Output is correct
27 Correct 23 ms 2560 KB Output is correct
28 Correct 142 ms 2600 KB Output is correct
29 Correct 23 ms 2560 KB Output is correct
30 Correct 143 ms 2596 KB Output is correct
31 Correct 23 ms 2560 KB Output is correct
32 Correct 146 ms 2560 KB Output is correct
33 Correct 24 ms 2560 KB Output is correct
34 Correct 13 ms 2560 KB Output is correct
35 Correct 23 ms 2560 KB Output is correct
36 Correct 145 ms 2560 KB Output is correct
37 Correct 23 ms 2560 KB Output is correct
38 Correct 141 ms 2560 KB Output is correct
39 Correct 22 ms 2560 KB Output is correct
40 Correct 145 ms 2596 KB Output is correct
41 Correct 32 ms 2560 KB Output is correct
42 Correct 147 ms 2560 KB Output is correct
43 Correct 26 ms 2560 KB Output is correct
44 Correct 157 ms 2680 KB Output is correct
45 Correct 23 ms 2560 KB Output is correct
46 Correct 149 ms 2592 KB Output is correct
47 Correct 3 ms 1920 KB Output is correct
48 Correct 86 ms 2560 KB Output is correct
49 Correct 83 ms 2560 KB Output is correct
50 Correct 157 ms 2560 KB Output is correct
51 Correct 172 ms 2560 KB Output is correct
52 Correct 12 ms 3072 KB Output is correct
53 Correct 11 ms 2816 KB Output is correct
54 Correct 13 ms 3072 KB Output is correct
55 Correct 11 ms 2816 KB Output is correct
56 Correct 12 ms 3048 KB Output is correct
57 Correct 11 ms 2816 KB Output is correct
58 Correct 12 ms 3072 KB Output is correct
59 Correct 11 ms 2816 KB Output is correct
60 Correct 12 ms 3072 KB Output is correct
61 Correct 11 ms 2816 KB Output is correct
62 Correct 12 ms 3072 KB Output is correct
63 Correct 11 ms 2816 KB Output is correct
64 Correct 16 ms 3028 KB Output is correct
65 Correct 11 ms 3072 KB Output is correct
66 Correct 11 ms 2816 KB Output is correct
67 Correct 12 ms 3072 KB Output is correct
68 Correct 11 ms 2816 KB Output is correct
69 Correct 12 ms 3072 KB Output is correct
70 Correct 16 ms 2816 KB Output is correct
71 Correct 12 ms 3072 KB Output is correct
72 Correct 13 ms 2816 KB Output is correct
73 Correct 17 ms 2944 KB Output is correct
74 Correct 2182 ms 5884 KB Output is correct
75 Correct 127 ms 5624 KB Output is correct
76 Correct 2309 ms 5876 KB Output is correct
77 Correct 114 ms 5624 KB Output is correct
78 Correct 2110 ms 5876 KB Output is correct
79 Correct 2151 ms 5880 KB Output is correct
80 Correct 114 ms 5624 KB Output is correct
81 Correct 130 ms 5624 KB Output is correct
82 Correct 2158 ms 5876 KB Output is correct
83 Correct 2181 ms 5884 KB Output is correct
84 Correct 130 ms 5640 KB Output is correct
85 Correct 2297 ms 5876 KB Output is correct
86 Correct 148 ms 5752 KB Output is correct
87 Correct 2176 ms 5876 KB Output is correct
88 Correct 114 ms 5624 KB Output is correct
89 Correct 2160 ms 5876 KB Output is correct
90 Correct 122 ms 5628 KB Output is correct
91 Correct 2147 ms 5876 KB Output is correct
92 Correct 126 ms 5624 KB Output is correct
93 Correct 2169 ms 5880 KB Output is correct
94 Correct 35 ms 4856 KB Output is correct
95 Correct 1090 ms 5880 KB Output is correct
96 Correct 1122 ms 5888 KB Output is correct
97 Correct 2241 ms 6772 KB Output is correct
98 Correct 2159 ms 6772 KB Output is correct