답안 #122148

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
122148 2019-06-27T17:27:24 Z duality Mergers (JOI19_mergers) C++11
100 / 100
2413 ms 170972 KB
#define DEBUG 0

#include <bits/stdc++.h>
using namespace std;

#if DEBUG
// basic debugging macros
int __i__,__j__;
#define printLine(l) for(__i__=0;__i__<l;__i__++){cout<<"-";}cout<<endl
#define printLine2(l,c) for(__i__=0;__i__<l;__i__++){cout<<c;}cout<<endl
#define printVar(n) cout<<#n<<": "<<n<<endl
#define printArr(a,l) cout<<#a<<": ";for(__i__=0;__i__<l;__i__++){cout<<a[__i__]<<" ";}cout<<endl
#define print2dArr(a,r,c) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<a[__i__][__j__]<<" ";}cout<<endl;}
#define print2dArr2(a,r,c,l) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<setw(l)<<setfill(' ')<<a[__i__][__j__]<<" ";}cout<<endl;}

// advanced debugging class
// debug 1,2,'A',"test";
class _Debug {
    public:
        template<typename T>
        _Debug& operator,(T val) {
            cout << val << endl;
            return *this;
        }
};
#define debug _Debug(),
#else
#define printLine(l)
#define printLine2(l,c)
#define printVar(n)
#define printArr(a,l)
#define print2dArr(a,r,c)
#define print2dArr2(a,r,c,l)
#define debug
#endif

// define
#define MAX_VAL 999999999
#define MAX_VAL_2 999999999999999999LL
#define EPS 1e-6
#define mp make_pair
#define pb push_back

// typedef
typedef unsigned int UI;
typedef long long int LLI;
typedef unsigned long long int ULLI;
typedef unsigned short int US;
typedef pair<int,int> pii;
typedef pair<LLI,LLI> plli;
typedef vector<int> vi;
typedef vector<LLI> vlli;
typedef vector<pii> vpii;
typedef vector<plli> vplli;

// ---------- END OF TEMPLATE ----------

vi adjList[500000];
vi states[500000];
int parent[500000][19],depth[500000];
int doDFS(int u,int p,int d) {
    int i;
    parent[u][0] = p,depth[u] = d;
    for (i = 0; i < adjList[u].size(); i++) {
        int v = adjList[u][i];
        if (v != p) doDFS(v,u,d+1);
    }
    return 0;
}
int toAdd[500000][19],yes[500000],yes2[500000],r[500000],ans = 0;
int doDFS2(int u,int p) {
    int i;
    int c = yes[u],n = -1,nc = 0;
    yes2[u] = c;
    for (i = 0; i < adjList[u].size(); i++) {
        int v = adjList[u][i];
        if (v != p) {
            doDFS2(v,u);
            yes[u] += yes[v];
            if (yes[v] > 0) n = v,nc++;
        }
    }
    if ((c == 1) && (yes[u] == c)) ans++,r[u] = 1;
    if ((nc == 1) && (r[n] || yes2[n])) r[u] = 1;
    return 0;
}
int main() {
    int i;
    int N,K;
    int A,B,S;
    scanf("%d %d",&N,&K);
    for (i = 0; i < N-1; i++) {
        scanf("%d %d",&A,&B);
        A--,B--;
        adjList[A].pb(B);
        adjList[B].pb(A);
    }
    for (i = 0; i < N; i++) scanf("%d",&S),states[S-1].pb(i);

    int j,k;
    doDFS(0,-1,0);
    for (i = 1; (1 << i) < N; i++) {
        for (j = 0; j < N; j++) {
            if (parent[j][i-1] != -1) parent[j][i] = parent[parent[j][i-1]][i-1];
            else parent[j][i] = -1;
        }
    }
    int logn = i;
    for (i = 0; i < K; i++) {
        for (j = 1; j < states[i].size(); j++) {
            int u = states[i][j-1],v = states[i][j];
            if (depth[u] < depth[v]) swap(u,v);
            for (k = logn-1; k >= 0; k--) {
                if ((parent[u][k] != -1) && (depth[parent[u][k]] >= depth[v]))
                    toAdd[u][k]++,u = parent[u][k];
            }
            if (u != v) {
                for (k = logn-1; k >= 0; k--) {
                    if (parent[u][k] != parent[v][k]) {
                        toAdd[u][k]++,u = parent[u][k];
                        toAdd[v][k]++,v = parent[v][k];
                    }
                }
                toAdd[u][0]++,toAdd[v][0]++;
            }
        }
    }
    for (i = logn-1; i >= 1; i--) {
        for (j = 0; j < N; j++) {
            if (parent[j][i] != -1) {
                toAdd[j][i-1] += toAdd[j][i];
                toAdd[parent[j][i-1]][i-1] += toAdd[j][i];
            }
        }
    }
    for (i = 0; i < N; i++) yes[i] = (i > 0) && (toAdd[i][0] == 0);
    doDFS2(0,-1);
    if (r[0]) ans++;
    printf("%d\n",(ans+1)/2);

    return 0;
}

Compilation message

mergers.cpp: In function 'int doDFS(int, int, int)':
mergers.cpp:64:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (i = 0; i < adjList[u].size(); i++) {
                 ~~^~~~~~~~~~~~~~~~~~~
mergers.cpp: In function 'int doDFS2(int, int)':
mergers.cpp:75:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (i = 0; i < adjList[u].size(); i++) {
                 ~~^~~~~~~~~~~~~~~~~~~
mergers.cpp: In function 'int main()':
mergers.cpp:110:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (j = 1; j < states[i].size(); j++) {
                     ~~^~~~~~~~~~~~~~~~~~
mergers.cpp:91:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d",&N,&K);
     ~~~~~^~~~~~~~~~~~~~~
mergers.cpp:93:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d",&A,&B);
         ~~~~~^~~~~~~~~~~~~~~
mergers.cpp:98:43: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for (i = 0; i < N; i++) scanf("%d",&S),states[S-1].pb(i);
                             ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 23808 KB Output is correct
2 Correct 21 ms 23936 KB Output is correct
3 Correct 21 ms 23936 KB Output is correct
4 Correct 22 ms 23936 KB Output is correct
5 Correct 21 ms 23936 KB Output is correct
6 Correct 21 ms 23936 KB Output is correct
7 Correct 21 ms 23808 KB Output is correct
8 Correct 22 ms 23808 KB Output is correct
9 Correct 21 ms 23812 KB Output is correct
10 Correct 20 ms 23892 KB Output is correct
11 Correct 20 ms 23808 KB Output is correct
12 Correct 21 ms 23800 KB Output is correct
13 Correct 21 ms 23808 KB Output is correct
14 Correct 21 ms 23928 KB Output is correct
15 Correct 21 ms 23808 KB Output is correct
16 Correct 21 ms 23936 KB Output is correct
17 Correct 20 ms 23808 KB Output is correct
18 Correct 21 ms 23800 KB Output is correct
19 Correct 20 ms 23808 KB Output is correct
20 Correct 22 ms 23936 KB Output is correct
21 Correct 20 ms 23800 KB Output is correct
22 Correct 22 ms 23936 KB Output is correct
23 Correct 21 ms 23780 KB Output is correct
24 Correct 20 ms 23928 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 23808 KB Output is correct
2 Correct 21 ms 23936 KB Output is correct
3 Correct 21 ms 23936 KB Output is correct
4 Correct 22 ms 23936 KB Output is correct
5 Correct 21 ms 23936 KB Output is correct
6 Correct 21 ms 23936 KB Output is correct
7 Correct 21 ms 23808 KB Output is correct
8 Correct 22 ms 23808 KB Output is correct
9 Correct 21 ms 23812 KB Output is correct
10 Correct 20 ms 23892 KB Output is correct
11 Correct 20 ms 23808 KB Output is correct
12 Correct 21 ms 23800 KB Output is correct
13 Correct 21 ms 23808 KB Output is correct
14 Correct 21 ms 23928 KB Output is correct
15 Correct 21 ms 23808 KB Output is correct
16 Correct 21 ms 23936 KB Output is correct
17 Correct 20 ms 23808 KB Output is correct
18 Correct 21 ms 23800 KB Output is correct
19 Correct 20 ms 23808 KB Output is correct
20 Correct 22 ms 23936 KB Output is correct
21 Correct 20 ms 23800 KB Output is correct
22 Correct 22 ms 23936 KB Output is correct
23 Correct 21 ms 23780 KB Output is correct
24 Correct 20 ms 23928 KB Output is correct
25 Correct 20 ms 23808 KB Output is correct
26 Correct 24 ms 24568 KB Output is correct
27 Correct 24 ms 24448 KB Output is correct
28 Correct 24 ms 24696 KB Output is correct
29 Correct 22 ms 24576 KB Output is correct
30 Correct 24 ms 24572 KB Output is correct
31 Correct 20 ms 23808 KB Output is correct
32 Correct 25 ms 24752 KB Output is correct
33 Correct 21 ms 23800 KB Output is correct
34 Correct 23 ms 24448 KB Output is correct
35 Correct 24 ms 24576 KB Output is correct
36 Correct 24 ms 24448 KB Output is correct
37 Correct 26 ms 24576 KB Output is correct
38 Correct 20 ms 23808 KB Output is correct
39 Correct 24 ms 24448 KB Output is correct
40 Correct 23 ms 24492 KB Output is correct
41 Correct 24 ms 24448 KB Output is correct
42 Correct 24 ms 24448 KB Output is correct
43 Correct 24 ms 24704 KB Output is correct
44 Correct 23 ms 23932 KB Output is correct
45 Correct 24 ms 24576 KB Output is correct
46 Correct 23 ms 24448 KB Output is correct
47 Correct 20 ms 23808 KB Output is correct
48 Correct 23 ms 24448 KB Output is correct
49 Correct 23 ms 24576 KB Output is correct
50 Correct 24 ms 24568 KB Output is correct
51 Correct 24 ms 24448 KB Output is correct
52 Correct 24 ms 24448 KB Output is correct
53 Correct 24 ms 24568 KB Output is correct
54 Correct 24 ms 24512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 23808 KB Output is correct
2 Correct 21 ms 23936 KB Output is correct
3 Correct 21 ms 23936 KB Output is correct
4 Correct 22 ms 23936 KB Output is correct
5 Correct 21 ms 23936 KB Output is correct
6 Correct 21 ms 23936 KB Output is correct
7 Correct 21 ms 23808 KB Output is correct
8 Correct 22 ms 23808 KB Output is correct
9 Correct 21 ms 23812 KB Output is correct
10 Correct 20 ms 23892 KB Output is correct
11 Correct 20 ms 23808 KB Output is correct
12 Correct 21 ms 23800 KB Output is correct
13 Correct 21 ms 23808 KB Output is correct
14 Correct 21 ms 23928 KB Output is correct
15 Correct 21 ms 23808 KB Output is correct
16 Correct 21 ms 23936 KB Output is correct
17 Correct 20 ms 23808 KB Output is correct
18 Correct 21 ms 23800 KB Output is correct
19 Correct 20 ms 23808 KB Output is correct
20 Correct 22 ms 23936 KB Output is correct
21 Correct 20 ms 23800 KB Output is correct
22 Correct 22 ms 23936 KB Output is correct
23 Correct 21 ms 23780 KB Output is correct
24 Correct 20 ms 23928 KB Output is correct
25 Correct 20 ms 23864 KB Output is correct
26 Correct 123 ms 45448 KB Output is correct
27 Correct 160 ms 45048 KB Output is correct
28 Correct 42 ms 24540 KB Output is correct
29 Correct 20 ms 23808 KB Output is correct
30 Correct 20 ms 23936 KB Output is correct
31 Correct 156 ms 45368 KB Output is correct
32 Correct 23 ms 24448 KB Output is correct
33 Correct 256 ms 52348 KB Output is correct
34 Correct 169 ms 45304 KB Output is correct
35 Correct 24 ms 24448 KB Output is correct
36 Correct 190 ms 46072 KB Output is correct
37 Correct 24 ms 24448 KB Output is correct
38 Correct 24 ms 24448 KB Output is correct
39 Correct 139 ms 45344 KB Output is correct
40 Correct 26 ms 24696 KB Output is correct
41 Correct 186 ms 45044 KB Output is correct
42 Correct 250 ms 47864 KB Output is correct
43 Correct 22 ms 23928 KB Output is correct
44 Correct 242 ms 52468 KB Output is correct
45 Correct 204 ms 48884 KB Output is correct
46 Correct 24 ms 24448 KB Output is correct
47 Correct 24 ms 24440 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 132 ms 45428 KB Output is correct
2 Correct 133 ms 48624 KB Output is correct
3 Correct 23 ms 24440 KB Output is correct
4 Correct 24 ms 24448 KB Output is correct
5 Correct 21 ms 23912 KB Output is correct
6 Correct 22 ms 23808 KB Output is correct
7 Correct 25 ms 24440 KB Output is correct
8 Correct 209 ms 46712 KB Output is correct
9 Correct 25 ms 24448 KB Output is correct
10 Correct 184 ms 45560 KB Output is correct
11 Correct 21 ms 23808 KB Output is correct
12 Correct 193 ms 45688 KB Output is correct
13 Correct 177 ms 46840 KB Output is correct
14 Correct 171 ms 48316 KB Output is correct
15 Correct 137 ms 45356 KB Output is correct
16 Correct 24 ms 24568 KB Output is correct
17 Correct 21 ms 23808 KB Output is correct
18 Correct 141 ms 48496 KB Output is correct
19 Correct 202 ms 52344 KB Output is correct
20 Correct 24 ms 24576 KB Output is correct
21 Correct 21 ms 23936 KB Output is correct
22 Correct 167 ms 47004 KB Output is correct
23 Correct 24 ms 24440 KB Output is correct
24 Correct 203 ms 45944 KB Output is correct
25 Correct 189 ms 50940 KB Output is correct
26 Correct 23 ms 24528 KB Output is correct
27 Correct 24 ms 24576 KB Output is correct
28 Correct 24 ms 24576 KB Output is correct
29 Correct 23 ms 24448 KB Output is correct
30 Correct 23 ms 24448 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 23808 KB Output is correct
2 Correct 21 ms 23936 KB Output is correct
3 Correct 21 ms 23936 KB Output is correct
4 Correct 22 ms 23936 KB Output is correct
5 Correct 21 ms 23936 KB Output is correct
6 Correct 21 ms 23936 KB Output is correct
7 Correct 21 ms 23808 KB Output is correct
8 Correct 22 ms 23808 KB Output is correct
9 Correct 21 ms 23812 KB Output is correct
10 Correct 20 ms 23892 KB Output is correct
11 Correct 20 ms 23808 KB Output is correct
12 Correct 21 ms 23800 KB Output is correct
13 Correct 21 ms 23808 KB Output is correct
14 Correct 21 ms 23928 KB Output is correct
15 Correct 21 ms 23808 KB Output is correct
16 Correct 21 ms 23936 KB Output is correct
17 Correct 20 ms 23808 KB Output is correct
18 Correct 21 ms 23800 KB Output is correct
19 Correct 20 ms 23808 KB Output is correct
20 Correct 22 ms 23936 KB Output is correct
21 Correct 20 ms 23800 KB Output is correct
22 Correct 22 ms 23936 KB Output is correct
23 Correct 21 ms 23780 KB Output is correct
24 Correct 20 ms 23928 KB Output is correct
25 Correct 20 ms 23808 KB Output is correct
26 Correct 24 ms 24568 KB Output is correct
27 Correct 24 ms 24448 KB Output is correct
28 Correct 24 ms 24696 KB Output is correct
29 Correct 22 ms 24576 KB Output is correct
30 Correct 24 ms 24572 KB Output is correct
31 Correct 20 ms 23808 KB Output is correct
32 Correct 25 ms 24752 KB Output is correct
33 Correct 21 ms 23800 KB Output is correct
34 Correct 23 ms 24448 KB Output is correct
35 Correct 24 ms 24576 KB Output is correct
36 Correct 24 ms 24448 KB Output is correct
37 Correct 26 ms 24576 KB Output is correct
38 Correct 20 ms 23808 KB Output is correct
39 Correct 24 ms 24448 KB Output is correct
40 Correct 23 ms 24492 KB Output is correct
41 Correct 24 ms 24448 KB Output is correct
42 Correct 24 ms 24448 KB Output is correct
43 Correct 24 ms 24704 KB Output is correct
44 Correct 23 ms 23932 KB Output is correct
45 Correct 24 ms 24576 KB Output is correct
46 Correct 23 ms 24448 KB Output is correct
47 Correct 20 ms 23808 KB Output is correct
48 Correct 23 ms 24448 KB Output is correct
49 Correct 23 ms 24576 KB Output is correct
50 Correct 24 ms 24568 KB Output is correct
51 Correct 24 ms 24448 KB Output is correct
52 Correct 24 ms 24448 KB Output is correct
53 Correct 24 ms 24568 KB Output is correct
54 Correct 24 ms 24512 KB Output is correct
55 Correct 20 ms 23864 KB Output is correct
56 Correct 123 ms 45448 KB Output is correct
57 Correct 160 ms 45048 KB Output is correct
58 Correct 42 ms 24540 KB Output is correct
59 Correct 20 ms 23808 KB Output is correct
60 Correct 20 ms 23936 KB Output is correct
61 Correct 156 ms 45368 KB Output is correct
62 Correct 23 ms 24448 KB Output is correct
63 Correct 256 ms 52348 KB Output is correct
64 Correct 169 ms 45304 KB Output is correct
65 Correct 24 ms 24448 KB Output is correct
66 Correct 190 ms 46072 KB Output is correct
67 Correct 24 ms 24448 KB Output is correct
68 Correct 24 ms 24448 KB Output is correct
69 Correct 139 ms 45344 KB Output is correct
70 Correct 26 ms 24696 KB Output is correct
71 Correct 186 ms 45044 KB Output is correct
72 Correct 250 ms 47864 KB Output is correct
73 Correct 22 ms 23928 KB Output is correct
74 Correct 242 ms 52468 KB Output is correct
75 Correct 204 ms 48884 KB Output is correct
76 Correct 24 ms 24448 KB Output is correct
77 Correct 24 ms 24440 KB Output is correct
78 Correct 132 ms 45428 KB Output is correct
79 Correct 133 ms 48624 KB Output is correct
80 Correct 23 ms 24440 KB Output is correct
81 Correct 24 ms 24448 KB Output is correct
82 Correct 21 ms 23912 KB Output is correct
83 Correct 22 ms 23808 KB Output is correct
84 Correct 25 ms 24440 KB Output is correct
85 Correct 209 ms 46712 KB Output is correct
86 Correct 25 ms 24448 KB Output is correct
87 Correct 184 ms 45560 KB Output is correct
88 Correct 21 ms 23808 KB Output is correct
89 Correct 193 ms 45688 KB Output is correct
90 Correct 177 ms 46840 KB Output is correct
91 Correct 171 ms 48316 KB Output is correct
92 Correct 137 ms 45356 KB Output is correct
93 Correct 24 ms 24568 KB Output is correct
94 Correct 21 ms 23808 KB Output is correct
95 Correct 141 ms 48496 KB Output is correct
96 Correct 202 ms 52344 KB Output is correct
97 Correct 24 ms 24576 KB Output is correct
98 Correct 21 ms 23936 KB Output is correct
99 Correct 167 ms 47004 KB Output is correct
100 Correct 24 ms 24440 KB Output is correct
101 Correct 203 ms 45944 KB Output is correct
102 Correct 189 ms 50940 KB Output is correct
103 Correct 23 ms 24528 KB Output is correct
104 Correct 24 ms 24576 KB Output is correct
105 Correct 24 ms 24576 KB Output is correct
106 Correct 23 ms 24448 KB Output is correct
107 Correct 23 ms 24448 KB Output is correct
108 Correct 1063 ms 140080 KB Output is correct
109 Correct 2364 ms 149836 KB Output is correct
110 Correct 2413 ms 159112 KB Output is correct
111 Correct 1815 ms 170972 KB Output is correct
112 Correct 1610 ms 161264 KB Output is correct
113 Correct 1039 ms 147808 KB Output is correct
114 Correct 1252 ms 130132 KB Output is correct
115 Correct 1275 ms 130068 KB Output is correct
116 Correct 1469 ms 136712 KB Output is correct
117 Correct 1229 ms 147940 KB Output is correct
118 Correct 1225 ms 131892 KB Output is correct
119 Correct 1257 ms 147876 KB Output is correct
120 Correct 1513 ms 161740 KB Output is correct
121 Correct 1407 ms 147816 KB Output is correct
122 Correct 1579 ms 141532 KB Output is correct
123 Correct 964 ms 149480 KB Output is correct
124 Correct 1430 ms 145220 KB Output is correct