답안 #146165

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
146165 2019-08-22T13:02:59 Z evpipis Tug of War (BOI15_tug) C++11
100 / 100
2198 ms 6788 KB
#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
typedef pair<int, int> ii;

const int len = 6e4+4, zer = 3e5;
int vis[len], par[len], dep[len], cost[len], fron[len], lef[len], cnt;
vector<ii> adj[len], vec;
bitset<2*zer+1> bit;

int check(int u){
    vis[u] = 1;

    int ans = 2-adj[u].size();
    for (int j = 0; j < adj[u].size(); j++){
        ii v = adj[u][j];
        if (!vis[v.fi])
            ans += check(v.fi), fron[v.se] = 1;
    }

    return ans;
}

ii dfs(int u){
    vis[u] = ++cnt;

    ii ans = mp(0, 0);
    for (int j = 0; j < adj[u].size(); j++){
        ii v = adj[u][j];
        if (!vis[v.fi]){
            //printf("front edge: u = %d, v.se = %d\n", u, v.se);
            par[v.fi] = v.se;
            if (u&1)
                dep[v.fi] = dep[u]+cost[v.se], ans.fi += cost[v.se];
            else
                dep[v.fi] = dep[u]-cost[v.se], ans.fi -= cost[v.se];

            ii cur = dfs(v.fi);
            ans.fi += cur.fi;
            ans.se += cur.se;
        }
        else if (vis[v.fi] < vis[u] && par[u] != v.se){
            //printf("back edge: u = %d, v.se = %d\n", u, v.se);
            int ed = -cost[v.se];
            if (u&1)
                ed = cost[v.se];

            ans.se += dep[u]-dep[v.fi]+ed;
            ans.fi += ed-ans.se;
        }
    }

    //printf("u = %d, dep = %d, ans = (%d, %d)\n", u, dep[u], ans.fi, ans.se);
    return ans;
}

int main(){
    int n, k;
    scanf("%d %d", &n, &k);
    for (int i = 1; i <= 2*n; i++){
        int a, b, c;
        scanf("%d %d %d", &a, &b, &c);
        cost[i] = c;
        lef[i] = 2*a-2;
        adj[2*a-2].pb(mp(2*b-1, i));
        adj[2*b-1].pb(mp(2*a-2, i));
    }

    for (int i = 1; i <= n; i++)
        if (!vis[2*i-2] && check(2*i-2) != 0){
            printf("NO\n");
            return 0;
        }

    for (int i = 0; i < 2*n; i++)
        vis[i] = 0;

    for (int i = 1; i <= 2*n; i++)
        if (!fron[i] && !vis[lef[i]])
            vec.pb(dfs(lef[i]));
    for (int i = 0; i < vec.size(); i++)
        vec[i].se = abs(vec[i].se);

    //for (int i = 0; i < vec.size(); i++)
      //  printf("(%d, %d)\n", vec[i].fi, vec[i].se);

    //vec.clear();
    //for (int i = 0; i < 100; i++)
      //  vec.pb(mp(0, 0));

    bit[zer] = 1;
    for (int i = 0; i < vec.size(); i++){
        if (vec[i].fi > 0)
            bit >>= (vec[i].fi);
        else if (vec[i].fi < 0)
            bit <<= (-vec[i].fi);

        if (vec[i].se)
            bit = (bit>>vec[i].se)|(bit<<vec[i].se);
    }

    for (int i = max(0, zer-k); i <= min(2*zer, zer+k); i++)
        if (bit[i]){
            printf("YES\n");
            return 0;
        }

    printf("NO\n");
    return 0;
}
/*
4 0
1 1 1
2 1 1
3 1 1
3 2 1
3 3 1
1 3 1
4 3 1
4 4 1
*/

Compilation message

tug.cpp: In function 'int check(int)':
tug.cpp:19:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int j = 0; j < adj[u].size(); j++){
                     ~~^~~~~~~~~~~~~~~
tug.cpp: In function 'ii dfs(int)':
tug.cpp:32:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int j = 0; j < adj[u].size(); j++){
                     ~~^~~~~~~~~~~~~~~
tug.cpp: In function 'int main()':
tug.cpp:85:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < vec.size(); i++)
                     ~~^~~~~~~~~~~~
tug.cpp:96:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < vec.size(); i++){
                     ~~^~~~~~~~~~~~
tug.cpp:63:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d", &n, &k);
     ~~~~~^~~~~~~~~~~~~~~~~
tug.cpp:66:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d %d", &a, &b, &c);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2168 KB Output is correct
2 Correct 4 ms 2044 KB Output is correct
3 Correct 4 ms 2040 KB Output is correct
4 Correct 4 ms 2168 KB Output is correct
5 Correct 4 ms 2168 KB Output is correct
6 Correct 4 ms 2040 KB Output is correct
7 Correct 4 ms 2040 KB Output is correct
8 Correct 4 ms 2040 KB Output is correct
9 Correct 4 ms 2040 KB Output is correct
10 Correct 4 ms 2040 KB Output is correct
11 Correct 4 ms 2040 KB Output is correct
12 Correct 4 ms 2040 KB Output is correct
13 Correct 5 ms 2168 KB Output is correct
14 Correct 5 ms 2168 KB Output is correct
15 Correct 5 ms 2168 KB Output is correct
16 Correct 4 ms 2168 KB Output is correct
17 Correct 4 ms 2040 KB Output is correct
18 Correct 5 ms 2168 KB Output is correct
19 Correct 4 ms 2040 KB Output is correct
20 Correct 4 ms 2168 KB Output is correct
21 Correct 3 ms 1784 KB Output is correct
22 Correct 4 ms 2040 KB Output is correct
23 Correct 4 ms 2168 KB Output is correct
24 Correct 4 ms 2036 KB Output is correct
25 Correct 4 ms 2168 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2168 KB Output is correct
2 Correct 4 ms 2044 KB Output is correct
3 Correct 4 ms 2040 KB Output is correct
4 Correct 4 ms 2168 KB Output is correct
5 Correct 4 ms 2168 KB Output is correct
6 Correct 4 ms 2040 KB Output is correct
7 Correct 4 ms 2040 KB Output is correct
8 Correct 4 ms 2040 KB Output is correct
9 Correct 4 ms 2040 KB Output is correct
10 Correct 4 ms 2040 KB Output is correct
11 Correct 4 ms 2040 KB Output is correct
12 Correct 4 ms 2040 KB Output is correct
13 Correct 5 ms 2168 KB Output is correct
14 Correct 5 ms 2168 KB Output is correct
15 Correct 5 ms 2168 KB Output is correct
16 Correct 4 ms 2168 KB Output is correct
17 Correct 4 ms 2040 KB Output is correct
18 Correct 5 ms 2168 KB Output is correct
19 Correct 4 ms 2040 KB Output is correct
20 Correct 4 ms 2168 KB Output is correct
21 Correct 3 ms 1784 KB Output is correct
22 Correct 4 ms 2040 KB Output is correct
23 Correct 4 ms 2168 KB Output is correct
24 Correct 4 ms 2036 KB Output is correct
25 Correct 4 ms 2168 KB Output is correct
26 Correct 141 ms 2424 KB Output is correct
27 Correct 25 ms 2424 KB Output is correct
28 Correct 143 ms 2552 KB Output is correct
29 Correct 26 ms 2436 KB Output is correct
30 Correct 143 ms 2424 KB Output is correct
31 Correct 25 ms 2432 KB Output is correct
32 Correct 144 ms 2456 KB Output is correct
33 Correct 26 ms 2424 KB Output is correct
34 Correct 16 ms 2296 KB Output is correct
35 Correct 25 ms 2424 KB Output is correct
36 Correct 139 ms 2456 KB Output is correct
37 Correct 26 ms 2552 KB Output is correct
38 Correct 142 ms 2424 KB Output is correct
39 Correct 26 ms 2424 KB Output is correct
40 Correct 145 ms 2424 KB Output is correct
41 Correct 25 ms 2424 KB Output is correct
42 Correct 143 ms 2424 KB Output is correct
43 Correct 25 ms 2424 KB Output is correct
44 Correct 140 ms 2552 KB Output is correct
45 Correct 25 ms 2428 KB Output is correct
46 Correct 142 ms 2424 KB Output is correct
47 Correct 6 ms 2012 KB Output is correct
48 Correct 85 ms 2424 KB Output is correct
49 Correct 91 ms 2424 KB Output is correct
50 Correct 149 ms 2660 KB Output is correct
51 Correct 149 ms 2424 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 3064 KB Output is correct
2 Correct 16 ms 2808 KB Output is correct
3 Correct 16 ms 3064 KB Output is correct
4 Correct 15 ms 2680 KB Output is correct
5 Correct 16 ms 3064 KB Output is correct
6 Correct 14 ms 2680 KB Output is correct
7 Correct 16 ms 3068 KB Output is correct
8 Correct 14 ms 2680 KB Output is correct
9 Correct 16 ms 3064 KB Output is correct
10 Correct 13 ms 2808 KB Output is correct
11 Correct 15 ms 3068 KB Output is correct
12 Correct 15 ms 2680 KB Output is correct
13 Correct 16 ms 3064 KB Output is correct
14 Correct 15 ms 3064 KB Output is correct
15 Correct 14 ms 2680 KB Output is correct
16 Correct 15 ms 3064 KB Output is correct
17 Correct 14 ms 2808 KB Output is correct
18 Correct 16 ms 3064 KB Output is correct
19 Correct 15 ms 2808 KB Output is correct
20 Correct 15 ms 3064 KB Output is correct
21 Correct 15 ms 2936 KB Output is correct
22 Correct 58 ms 3064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2168 KB Output is correct
2 Correct 4 ms 2044 KB Output is correct
3 Correct 4 ms 2040 KB Output is correct
4 Correct 4 ms 2168 KB Output is correct
5 Correct 4 ms 2168 KB Output is correct
6 Correct 4 ms 2040 KB Output is correct
7 Correct 4 ms 2040 KB Output is correct
8 Correct 4 ms 2040 KB Output is correct
9 Correct 4 ms 2040 KB Output is correct
10 Correct 4 ms 2040 KB Output is correct
11 Correct 4 ms 2040 KB Output is correct
12 Correct 4 ms 2040 KB Output is correct
13 Correct 5 ms 2168 KB Output is correct
14 Correct 5 ms 2168 KB Output is correct
15 Correct 5 ms 2168 KB Output is correct
16 Correct 4 ms 2168 KB Output is correct
17 Correct 4 ms 2040 KB Output is correct
18 Correct 5 ms 2168 KB Output is correct
19 Correct 4 ms 2040 KB Output is correct
20 Correct 4 ms 2168 KB Output is correct
21 Correct 3 ms 1784 KB Output is correct
22 Correct 4 ms 2040 KB Output is correct
23 Correct 4 ms 2168 KB Output is correct
24 Correct 4 ms 2036 KB Output is correct
25 Correct 4 ms 2168 KB Output is correct
26 Correct 141 ms 2424 KB Output is correct
27 Correct 25 ms 2424 KB Output is correct
28 Correct 143 ms 2552 KB Output is correct
29 Correct 26 ms 2436 KB Output is correct
30 Correct 143 ms 2424 KB Output is correct
31 Correct 25 ms 2432 KB Output is correct
32 Correct 144 ms 2456 KB Output is correct
33 Correct 26 ms 2424 KB Output is correct
34 Correct 16 ms 2296 KB Output is correct
35 Correct 25 ms 2424 KB Output is correct
36 Correct 139 ms 2456 KB Output is correct
37 Correct 26 ms 2552 KB Output is correct
38 Correct 142 ms 2424 KB Output is correct
39 Correct 26 ms 2424 KB Output is correct
40 Correct 145 ms 2424 KB Output is correct
41 Correct 25 ms 2424 KB Output is correct
42 Correct 143 ms 2424 KB Output is correct
43 Correct 25 ms 2424 KB Output is correct
44 Correct 140 ms 2552 KB Output is correct
45 Correct 25 ms 2428 KB Output is correct
46 Correct 142 ms 2424 KB Output is correct
47 Correct 6 ms 2012 KB Output is correct
48 Correct 85 ms 2424 KB Output is correct
49 Correct 91 ms 2424 KB Output is correct
50 Correct 149 ms 2660 KB Output is correct
51 Correct 149 ms 2424 KB Output is correct
52 Correct 16 ms 3064 KB Output is correct
53 Correct 16 ms 2808 KB Output is correct
54 Correct 16 ms 3064 KB Output is correct
55 Correct 15 ms 2680 KB Output is correct
56 Correct 16 ms 3064 KB Output is correct
57 Correct 14 ms 2680 KB Output is correct
58 Correct 16 ms 3068 KB Output is correct
59 Correct 14 ms 2680 KB Output is correct
60 Correct 16 ms 3064 KB Output is correct
61 Correct 13 ms 2808 KB Output is correct
62 Correct 15 ms 3068 KB Output is correct
63 Correct 15 ms 2680 KB Output is correct
64 Correct 16 ms 3064 KB Output is correct
65 Correct 15 ms 3064 KB Output is correct
66 Correct 14 ms 2680 KB Output is correct
67 Correct 15 ms 3064 KB Output is correct
68 Correct 14 ms 2808 KB Output is correct
69 Correct 16 ms 3064 KB Output is correct
70 Correct 15 ms 2808 KB Output is correct
71 Correct 15 ms 3064 KB Output is correct
72 Correct 15 ms 2936 KB Output is correct
73 Correct 58 ms 3064 KB Output is correct
74 Correct 2145 ms 6688 KB Output is correct
75 Correct 129 ms 6136 KB Output is correct
76 Correct 2078 ms 6772 KB Output is correct
77 Correct 129 ms 6136 KB Output is correct
78 Correct 2084 ms 6516 KB Output is correct
79 Correct 2084 ms 6516 KB Output is correct
80 Correct 134 ms 6308 KB Output is correct
81 Correct 133 ms 6140 KB Output is correct
82 Correct 2185 ms 6512 KB Output is correct
83 Correct 2059 ms 6516 KB Output is correct
84 Correct 127 ms 6292 KB Output is correct
85 Correct 2104 ms 6516 KB Output is correct
86 Correct 127 ms 6136 KB Output is correct
87 Correct 2084 ms 6516 KB Output is correct
88 Correct 125 ms 6264 KB Output is correct
89 Correct 2084 ms 6640 KB Output is correct
90 Correct 129 ms 6392 KB Output is correct
91 Correct 2089 ms 6516 KB Output is correct
92 Correct 131 ms 6136 KB Output is correct
93 Correct 2060 ms 6644 KB Output is correct
94 Correct 41 ms 5112 KB Output is correct
95 Correct 1240 ms 6788 KB Output is correct
96 Correct 1230 ms 6788 KB Output is correct
97 Correct 2198 ms 6516 KB Output is correct
98 Correct 2178 ms 6520 KB Output is correct