답안 #226349

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
226349 2020-04-23T12:23:32 Z MKopchev Tug of War (BOI15_tug) C++14
100 / 100
1543 ms 11256 KB
#include<bits/stdc++.h>
using namespace std;
const int nmax=6e4+42;

int n,k;

struct edge
{
    int l,r,add;
};

edge inp[nmax];

set<int> adj[nmax];

void stop(bool ok)
{
    if(ok)printf("YES\n");
    else printf("NO\n");
    exit(0);
}

bool solved[nmax];

queue< pair<int/*size*/,int/*id*/> > to_place;

void cut_edge(int edge)
{
    //cout<<"cut_edge "<<edge<<endl;

    adj[inp[edge].l].erase(edge);
    adj[inp[edge].r].erase(edge);

    if(adj[inp[edge].l].size()<=1)to_place.push({adj[inp[edge].l].size(),inp[edge].l});
    if(adj[inp[edge].r].size()<=1)to_place.push({adj[inp[edge].r].size(),inp[edge].r});
}

bitset<10*nmax> can;

int main()
{
    scanf("%i%i",&n,&k);

    int total=0;

    int LHS=0,RHS=0;

    for(int i=1;i<=2*n;i++)
    {
        scanf("%i%i%i",&inp[i].l,&inp[i].r,&inp[i].add);
        inp[i].r+=n;

        adj[inp[i].l].insert(i);
        adj[inp[i].r].insert(i);

        total+=inp[i].add;
    }

    for(int i=1;i<=2*n;i++)
        if(adj[i].size()<=1)to_place.push({adj[i].size(),i});

    while(to_place.size())
    {
        pair<int/*size*/,int/*id*/> tp=to_place.front();
        to_place.pop();

        //cout<<"test "<<tp.second<<endl;

        if(solved[tp.second])continue;

        if(adj[tp.second].size()!=tp.first)continue;

        if(tp.first==0)stop(0);

        //cout<<"! forced "<<tp.second<<endl;

        solved[tp.second]=1;

        if(tp.second<=n)LHS+=inp[*adj[tp.second].begin()].add;
        else RHS+=inp[*adj[tp.second].begin()].add;

        cut_edge(*adj[tp.second].begin());
    }

    if(LHS<=total/2)can[LHS]=1;
    if(RHS<=total/2)can[RHS]=1;

    //cout<<LHS<<" "<<RHS<<endl;

    //cycles of even length
    for(int i=1;i<=2*n;i++)
        if(solved[i]==0)
        {
            int sum[2]={0,0};

            int pos=i,where=0;

            while(adj[pos].size())
            {
                solved[pos]=1;
                int edge=*adj[pos].begin();

                sum[where]+=inp[edge].add;
                where=!where;

                adj[inp[edge].l].erase(edge);
                adj[inp[edge].r].erase(edge);

                pos=inp[edge].l+inp[edge].r-pos;
            }

            //cout<<"pick "<<sum[0]<<" "<<sum[1]<<endl;

            can=(can<<sum[0])|(can<<sum[1]);
            /*
            for(int j=0;j<=total;j++)
                if(can[j])cout<<j<<" ";cout<<endl;
            */
        }

    for(int i=0;i<=total/2;i++)
        if(can[i]&&abs(total-2*i)<=k)stop(1);

    stop(0);
    return 0;
}

Compilation message

tug.cpp: In function 'int main()':
tug.cpp:71:33: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         if(adj[tp.second].size()!=tp.first)continue;
            ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~
tug.cpp:42:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%i%i",&n,&k);
     ~~~~~^~~~~~~~~~~~~~
tug.cpp:50:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%i%i%i",&inp[i].l,&inp[i].r,&inp[i].add);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 3456 KB Output is correct
2 Correct 7 ms 3584 KB Output is correct
3 Correct 7 ms 3584 KB Output is correct
4 Correct 7 ms 3584 KB Output is correct
5 Correct 7 ms 3584 KB Output is correct
6 Correct 8 ms 3456 KB Output is correct
7 Correct 7 ms 3584 KB Output is correct
8 Correct 7 ms 3456 KB Output is correct
9 Correct 7 ms 3456 KB Output is correct
10 Correct 7 ms 3456 KB Output is correct
11 Correct 7 ms 3584 KB Output is correct
12 Correct 7 ms 3584 KB Output is correct
13 Correct 6 ms 3456 KB Output is correct
14 Correct 7 ms 3584 KB Output is correct
15 Correct 7 ms 3456 KB Output is correct
16 Correct 7 ms 3584 KB Output is correct
17 Correct 7 ms 3456 KB Output is correct
18 Correct 8 ms 3456 KB Output is correct
19 Correct 7 ms 3456 KB Output is correct
20 Correct 7 ms 3456 KB Output is correct
21 Correct 6 ms 3200 KB Output is correct
22 Correct 7 ms 3584 KB Output is correct
23 Correct 7 ms 3584 KB Output is correct
24 Correct 7 ms 3456 KB Output is correct
25 Correct 7 ms 3584 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 3456 KB Output is correct
2 Correct 7 ms 3584 KB Output is correct
3 Correct 7 ms 3584 KB Output is correct
4 Correct 7 ms 3584 KB Output is correct
5 Correct 7 ms 3584 KB Output is correct
6 Correct 8 ms 3456 KB Output is correct
7 Correct 7 ms 3584 KB Output is correct
8 Correct 7 ms 3456 KB Output is correct
9 Correct 7 ms 3456 KB Output is correct
10 Correct 7 ms 3456 KB Output is correct
11 Correct 7 ms 3584 KB Output is correct
12 Correct 7 ms 3584 KB Output is correct
13 Correct 6 ms 3456 KB Output is correct
14 Correct 7 ms 3584 KB Output is correct
15 Correct 7 ms 3456 KB Output is correct
16 Correct 7 ms 3584 KB Output is correct
17 Correct 7 ms 3456 KB Output is correct
18 Correct 8 ms 3456 KB Output is correct
19 Correct 7 ms 3456 KB Output is correct
20 Correct 7 ms 3456 KB Output is correct
21 Correct 6 ms 3200 KB Output is correct
22 Correct 7 ms 3584 KB Output is correct
23 Correct 7 ms 3584 KB Output is correct
24 Correct 7 ms 3456 KB Output is correct
25 Correct 7 ms 3584 KB Output is correct
26 Correct 104 ms 3968 KB Output is correct
27 Correct 21 ms 3968 KB Output is correct
28 Correct 102 ms 3968 KB Output is correct
29 Correct 21 ms 3968 KB Output is correct
30 Correct 108 ms 3968 KB Output is correct
31 Correct 21 ms 3968 KB Output is correct
32 Correct 98 ms 3968 KB Output is correct
33 Correct 21 ms 3968 KB Output is correct
34 Correct 15 ms 3968 KB Output is correct
35 Correct 21 ms 3968 KB Output is correct
36 Correct 107 ms 3968 KB Output is correct
37 Correct 21 ms 3968 KB Output is correct
38 Correct 107 ms 3968 KB Output is correct
39 Correct 22 ms 3968 KB Output is correct
40 Correct 99 ms 3968 KB Output is correct
41 Correct 22 ms 3968 KB Output is correct
42 Correct 101 ms 3984 KB Output is correct
43 Correct 22 ms 3968 KB Output is correct
44 Correct 100 ms 3968 KB Output is correct
45 Correct 22 ms 3968 KB Output is correct
46 Correct 100 ms 3968 KB Output is correct
47 Correct 8 ms 3584 KB Output is correct
48 Correct 57 ms 3968 KB Output is correct
49 Correct 55 ms 4124 KB Output is correct
50 Correct 109 ms 3968 KB Output is correct
51 Correct 112 ms 3968 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 512 ms 5752 KB Output is correct
2 Correct 17 ms 5376 KB Output is correct
3 Correct 503 ms 5632 KB Output is correct
4 Correct 20 ms 5248 KB Output is correct
5 Correct 532 ms 5752 KB Output is correct
6 Correct 17 ms 5376 KB Output is correct
7 Correct 490 ms 5688 KB Output is correct
8 Correct 17 ms 5248 KB Output is correct
9 Correct 464 ms 5632 KB Output is correct
10 Correct 17 ms 5248 KB Output is correct
11 Correct 518 ms 5632 KB Output is correct
12 Correct 17 ms 5248 KB Output is correct
13 Correct 487 ms 5752 KB Output is correct
14 Correct 469 ms 5752 KB Output is correct
15 Correct 17 ms 5376 KB Output is correct
16 Correct 479 ms 5632 KB Output is correct
17 Correct 18 ms 5248 KB Output is correct
18 Correct 476 ms 5752 KB Output is correct
19 Correct 18 ms 5248 KB Output is correct
20 Correct 502 ms 5752 KB Output is correct
21 Correct 18 ms 5376 KB Output is correct
22 Correct 260 ms 5760 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 3456 KB Output is correct
2 Correct 7 ms 3584 KB Output is correct
3 Correct 7 ms 3584 KB Output is correct
4 Correct 7 ms 3584 KB Output is correct
5 Correct 7 ms 3584 KB Output is correct
6 Correct 8 ms 3456 KB Output is correct
7 Correct 7 ms 3584 KB Output is correct
8 Correct 7 ms 3456 KB Output is correct
9 Correct 7 ms 3456 KB Output is correct
10 Correct 7 ms 3456 KB Output is correct
11 Correct 7 ms 3584 KB Output is correct
12 Correct 7 ms 3584 KB Output is correct
13 Correct 6 ms 3456 KB Output is correct
14 Correct 7 ms 3584 KB Output is correct
15 Correct 7 ms 3456 KB Output is correct
16 Correct 7 ms 3584 KB Output is correct
17 Correct 7 ms 3456 KB Output is correct
18 Correct 8 ms 3456 KB Output is correct
19 Correct 7 ms 3456 KB Output is correct
20 Correct 7 ms 3456 KB Output is correct
21 Correct 6 ms 3200 KB Output is correct
22 Correct 7 ms 3584 KB Output is correct
23 Correct 7 ms 3584 KB Output is correct
24 Correct 7 ms 3456 KB Output is correct
25 Correct 7 ms 3584 KB Output is correct
26 Correct 104 ms 3968 KB Output is correct
27 Correct 21 ms 3968 KB Output is correct
28 Correct 102 ms 3968 KB Output is correct
29 Correct 21 ms 3968 KB Output is correct
30 Correct 108 ms 3968 KB Output is correct
31 Correct 21 ms 3968 KB Output is correct
32 Correct 98 ms 3968 KB Output is correct
33 Correct 21 ms 3968 KB Output is correct
34 Correct 15 ms 3968 KB Output is correct
35 Correct 21 ms 3968 KB Output is correct
36 Correct 107 ms 3968 KB Output is correct
37 Correct 21 ms 3968 KB Output is correct
38 Correct 107 ms 3968 KB Output is correct
39 Correct 22 ms 3968 KB Output is correct
40 Correct 99 ms 3968 KB Output is correct
41 Correct 22 ms 3968 KB Output is correct
42 Correct 101 ms 3984 KB Output is correct
43 Correct 22 ms 3968 KB Output is correct
44 Correct 100 ms 3968 KB Output is correct
45 Correct 22 ms 3968 KB Output is correct
46 Correct 100 ms 3968 KB Output is correct
47 Correct 8 ms 3584 KB Output is correct
48 Correct 57 ms 3968 KB Output is correct
49 Correct 55 ms 4124 KB Output is correct
50 Correct 109 ms 3968 KB Output is correct
51 Correct 112 ms 3968 KB Output is correct
52 Correct 512 ms 5752 KB Output is correct
53 Correct 17 ms 5376 KB Output is correct
54 Correct 503 ms 5632 KB Output is correct
55 Correct 20 ms 5248 KB Output is correct
56 Correct 532 ms 5752 KB Output is correct
57 Correct 17 ms 5376 KB Output is correct
58 Correct 490 ms 5688 KB Output is correct
59 Correct 17 ms 5248 KB Output is correct
60 Correct 464 ms 5632 KB Output is correct
61 Correct 17 ms 5248 KB Output is correct
62 Correct 518 ms 5632 KB Output is correct
63 Correct 17 ms 5248 KB Output is correct
64 Correct 487 ms 5752 KB Output is correct
65 Correct 469 ms 5752 KB Output is correct
66 Correct 17 ms 5376 KB Output is correct
67 Correct 479 ms 5632 KB Output is correct
68 Correct 18 ms 5248 KB Output is correct
69 Correct 476 ms 5752 KB Output is correct
70 Correct 18 ms 5248 KB Output is correct
71 Correct 502 ms 5752 KB Output is correct
72 Correct 18 ms 5376 KB Output is correct
73 Correct 260 ms 5760 KB Output is correct
74 Correct 1473 ms 10360 KB Output is correct
75 Correct 112 ms 9848 KB Output is correct
76 Correct 1493 ms 10232 KB Output is correct
77 Correct 111 ms 10744 KB Output is correct
78 Correct 1511 ms 11104 KB Output is correct
79 Correct 1445 ms 11004 KB Output is correct
80 Correct 110 ms 10744 KB Output is correct
81 Correct 119 ms 10872 KB Output is correct
82 Correct 1446 ms 10976 KB Output is correct
83 Correct 1423 ms 10872 KB Output is correct
84 Correct 116 ms 10744 KB Output is correct
85 Correct 1443 ms 11000 KB Output is correct
86 Correct 115 ms 10796 KB Output is correct
87 Correct 1456 ms 10872 KB Output is correct
88 Correct 120 ms 10872 KB Output is correct
89 Correct 1543 ms 10884 KB Output is correct
90 Correct 114 ms 10744 KB Output is correct
91 Correct 1405 ms 11000 KB Output is correct
92 Correct 121 ms 10744 KB Output is correct
93 Correct 1450 ms 11256 KB Output is correct
94 Correct 52 ms 10488 KB Output is correct
95 Correct 787 ms 10996 KB Output is correct
96 Correct 780 ms 11000 KB Output is correct
97 Correct 1496 ms 10888 KB Output is correct
98 Correct 1475 ms 11000 KB Output is correct