답안 #122243

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
122243 2019-06-27T23:38:36 Z duality Mergers (JOI19_mergers) C++11
100 / 100
968 ms 81756 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],depth[500000];
int doDFS(int u,int p,int d) {
    int i;
    parent[u] = 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 ds[500000],deg[500000];
int find(int n) {
    if (ds[n] != n) ds[n] = find(ds[n]);
    return ds[n];
}
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;
    doDFS(0,-1,0);
    for (i = 0; i < N; i++) ds[i] = i;
    for (i = 0; i < K; i++) {
        for (j = 1; j < states[i].size(); j++) {
            int u = find(states[i][j-1]),v = find(states[i][j]);
            while (u != v) {
                if (depth[u] < depth[v]) swap(u,v);
                ds[u] = find(parent[u]),u = find(u);
            }
        }
    }
    int ans = 0;
    for (i = 0; i < N; i++) {
        for (j = 0; j < adjList[i].size(); j++) {
            int v = adjList[i][j];
            if (find(i) != find(v)) deg[find(i)]++;
        }
    }
    for (i = 0; i < N; i++) {
        if ((ds[i] == i) && (deg[i] == 1)) 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 main()':
mergers.cpp:92:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (j = 1; j < states[i].size(); j++) {
                     ~~^~~~~~~~~~~~~~~~~~
mergers.cpp:102:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (j = 0; j < adjList[i].size(); j++) {
                     ~~^~~~~~~~~~~~~~~~~~~
mergers.cpp:79: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:81: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:86: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 23 ms 23800 KB Output is correct
2 Correct 21 ms 23800 KB Output is correct
3 Correct 21 ms 23808 KB Output is correct
4 Correct 22 ms 23800 KB Output is correct
5 Correct 21 ms 23808 KB Output is correct
6 Correct 22 ms 23808 KB Output is correct
7 Correct 22 ms 23808 KB Output is correct
8 Correct 22 ms 23928 KB Output is correct
9 Correct 21 ms 23808 KB Output is correct
10 Correct 20 ms 23808 KB Output is correct
11 Correct 22 ms 23808 KB Output is correct
12 Correct 23 ms 23808 KB Output is correct
13 Correct 22 ms 23892 KB Output is correct
14 Correct 21 ms 23808 KB Output is correct
15 Correct 22 ms 23808 KB Output is correct
16 Correct 21 ms 23808 KB Output is correct
17 Correct 21 ms 23928 KB Output is correct
18 Correct 21 ms 23800 KB Output is correct
19 Correct 23 ms 23800 KB Output is correct
20 Correct 22 ms 23768 KB Output is correct
21 Correct 22 ms 23808 KB Output is correct
22 Correct 23 ms 23808 KB Output is correct
23 Correct 22 ms 23808 KB Output is correct
24 Correct 23 ms 23808 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 23800 KB Output is correct
2 Correct 21 ms 23800 KB Output is correct
3 Correct 21 ms 23808 KB Output is correct
4 Correct 22 ms 23800 KB Output is correct
5 Correct 21 ms 23808 KB Output is correct
6 Correct 22 ms 23808 KB Output is correct
7 Correct 22 ms 23808 KB Output is correct
8 Correct 22 ms 23928 KB Output is correct
9 Correct 21 ms 23808 KB Output is correct
10 Correct 20 ms 23808 KB Output is correct
11 Correct 22 ms 23808 KB Output is correct
12 Correct 23 ms 23808 KB Output is correct
13 Correct 22 ms 23892 KB Output is correct
14 Correct 21 ms 23808 KB Output is correct
15 Correct 22 ms 23808 KB Output is correct
16 Correct 21 ms 23808 KB Output is correct
17 Correct 21 ms 23928 KB Output is correct
18 Correct 21 ms 23800 KB Output is correct
19 Correct 23 ms 23800 KB Output is correct
20 Correct 22 ms 23768 KB Output is correct
21 Correct 22 ms 23808 KB Output is correct
22 Correct 23 ms 23808 KB Output is correct
23 Correct 22 ms 23808 KB Output is correct
24 Correct 23 ms 23808 KB Output is correct
25 Correct 23 ms 23808 KB Output is correct
26 Correct 26 ms 24064 KB Output is correct
27 Correct 25 ms 24064 KB Output is correct
28 Correct 25 ms 24164 KB Output is correct
29 Correct 24 ms 24064 KB Output is correct
30 Correct 24 ms 24064 KB Output is correct
31 Correct 21 ms 23896 KB Output is correct
32 Correct 23 ms 24192 KB Output is correct
33 Correct 21 ms 23808 KB Output is correct
34 Correct 24 ms 24056 KB Output is correct
35 Correct 25 ms 24064 KB Output is correct
36 Correct 24 ms 24064 KB Output is correct
37 Correct 24 ms 24064 KB Output is correct
38 Correct 22 ms 23808 KB Output is correct
39 Correct 27 ms 24056 KB Output is correct
40 Correct 23 ms 23936 KB Output is correct
41 Correct 24 ms 24056 KB Output is correct
42 Correct 24 ms 24064 KB Output is correct
43 Correct 23 ms 24192 KB Output is correct
44 Correct 21 ms 23808 KB Output is correct
45 Correct 24 ms 24064 KB Output is correct
46 Correct 24 ms 24064 KB Output is correct
47 Correct 21 ms 23808 KB Output is correct
48 Correct 24 ms 24192 KB Output is correct
49 Correct 23 ms 24064 KB Output is correct
50 Correct 23 ms 24192 KB Output is correct
51 Correct 24 ms 24064 KB Output is correct
52 Correct 24 ms 23972 KB Output is correct
53 Correct 24 ms 24192 KB Output is correct
54 Correct 24 ms 24056 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 23800 KB Output is correct
2 Correct 21 ms 23800 KB Output is correct
3 Correct 21 ms 23808 KB Output is correct
4 Correct 22 ms 23800 KB Output is correct
5 Correct 21 ms 23808 KB Output is correct
6 Correct 22 ms 23808 KB Output is correct
7 Correct 22 ms 23808 KB Output is correct
8 Correct 22 ms 23928 KB Output is correct
9 Correct 21 ms 23808 KB Output is correct
10 Correct 20 ms 23808 KB Output is correct
11 Correct 22 ms 23808 KB Output is correct
12 Correct 23 ms 23808 KB Output is correct
13 Correct 22 ms 23892 KB Output is correct
14 Correct 21 ms 23808 KB Output is correct
15 Correct 22 ms 23808 KB Output is correct
16 Correct 21 ms 23808 KB Output is correct
17 Correct 21 ms 23928 KB Output is correct
18 Correct 21 ms 23800 KB Output is correct
19 Correct 23 ms 23800 KB Output is correct
20 Correct 22 ms 23768 KB Output is correct
21 Correct 22 ms 23808 KB Output is correct
22 Correct 23 ms 23808 KB Output is correct
23 Correct 22 ms 23808 KB Output is correct
24 Correct 23 ms 23808 KB Output is correct
25 Correct 21 ms 23808 KB Output is correct
26 Correct 81 ms 30648 KB Output is correct
27 Correct 104 ms 30268 KB Output is correct
28 Correct 24 ms 24060 KB Output is correct
29 Correct 21 ms 23808 KB Output is correct
30 Correct 22 ms 23808 KB Output is correct
31 Correct 115 ms 30200 KB Output is correct
32 Correct 24 ms 24064 KB Output is correct
33 Correct 144 ms 34424 KB Output is correct
34 Correct 132 ms 30200 KB Output is correct
35 Correct 25 ms 24056 KB Output is correct
36 Correct 124 ms 30756 KB Output is correct
37 Correct 25 ms 24064 KB Output is correct
38 Correct 29 ms 24056 KB Output is correct
39 Correct 84 ms 30448 KB Output is correct
40 Correct 25 ms 24188 KB Output is correct
41 Correct 105 ms 30208 KB Output is correct
42 Correct 111 ms 31480 KB Output is correct
43 Correct 21 ms 23808 KB Output is correct
44 Correct 118 ms 34548 KB Output is correct
45 Correct 105 ms 32376 KB Output is correct
46 Correct 25 ms 24056 KB Output is correct
47 Correct 24 ms 24056 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 82 ms 29168 KB Output is correct
2 Correct 110 ms 32112 KB Output is correct
3 Correct 24 ms 24064 KB Output is correct
4 Correct 25 ms 24056 KB Output is correct
5 Correct 23 ms 23808 KB Output is correct
6 Correct 23 ms 23808 KB Output is correct
7 Correct 24 ms 24056 KB Output is correct
8 Correct 123 ms 31864 KB Output is correct
9 Correct 24 ms 24064 KB Output is correct
10 Correct 105 ms 30840 KB Output is correct
11 Correct 22 ms 23808 KB Output is correct
12 Correct 116 ms 30712 KB Output is correct
13 Correct 123 ms 31820 KB Output is correct
14 Correct 117 ms 33436 KB Output is correct
15 Correct 84 ms 30420 KB Output is correct
16 Correct 24 ms 24064 KB Output is correct
17 Correct 23 ms 23808 KB Output is correct
18 Correct 99 ms 33516 KB Output is correct
19 Correct 129 ms 35832 KB Output is correct
20 Correct 25 ms 24064 KB Output is correct
21 Correct 22 ms 23928 KB Output is correct
22 Correct 112 ms 32084 KB Output is correct
23 Correct 24 ms 24056 KB Output is correct
24 Correct 130 ms 31224 KB Output is correct
25 Correct 128 ms 34936 KB Output is correct
26 Correct 24 ms 24064 KB Output is correct
27 Correct 24 ms 24192 KB Output is correct
28 Correct 24 ms 24064 KB Output is correct
29 Correct 24 ms 24056 KB Output is correct
30 Correct 24 ms 24064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 23800 KB Output is correct
2 Correct 21 ms 23800 KB Output is correct
3 Correct 21 ms 23808 KB Output is correct
4 Correct 22 ms 23800 KB Output is correct
5 Correct 21 ms 23808 KB Output is correct
6 Correct 22 ms 23808 KB Output is correct
7 Correct 22 ms 23808 KB Output is correct
8 Correct 22 ms 23928 KB Output is correct
9 Correct 21 ms 23808 KB Output is correct
10 Correct 20 ms 23808 KB Output is correct
11 Correct 22 ms 23808 KB Output is correct
12 Correct 23 ms 23808 KB Output is correct
13 Correct 22 ms 23892 KB Output is correct
14 Correct 21 ms 23808 KB Output is correct
15 Correct 22 ms 23808 KB Output is correct
16 Correct 21 ms 23808 KB Output is correct
17 Correct 21 ms 23928 KB Output is correct
18 Correct 21 ms 23800 KB Output is correct
19 Correct 23 ms 23800 KB Output is correct
20 Correct 22 ms 23768 KB Output is correct
21 Correct 22 ms 23808 KB Output is correct
22 Correct 23 ms 23808 KB Output is correct
23 Correct 22 ms 23808 KB Output is correct
24 Correct 23 ms 23808 KB Output is correct
25 Correct 23 ms 23808 KB Output is correct
26 Correct 26 ms 24064 KB Output is correct
27 Correct 25 ms 24064 KB Output is correct
28 Correct 25 ms 24164 KB Output is correct
29 Correct 24 ms 24064 KB Output is correct
30 Correct 24 ms 24064 KB Output is correct
31 Correct 21 ms 23896 KB Output is correct
32 Correct 23 ms 24192 KB Output is correct
33 Correct 21 ms 23808 KB Output is correct
34 Correct 24 ms 24056 KB Output is correct
35 Correct 25 ms 24064 KB Output is correct
36 Correct 24 ms 24064 KB Output is correct
37 Correct 24 ms 24064 KB Output is correct
38 Correct 22 ms 23808 KB Output is correct
39 Correct 27 ms 24056 KB Output is correct
40 Correct 23 ms 23936 KB Output is correct
41 Correct 24 ms 24056 KB Output is correct
42 Correct 24 ms 24064 KB Output is correct
43 Correct 23 ms 24192 KB Output is correct
44 Correct 21 ms 23808 KB Output is correct
45 Correct 24 ms 24064 KB Output is correct
46 Correct 24 ms 24064 KB Output is correct
47 Correct 21 ms 23808 KB Output is correct
48 Correct 24 ms 24192 KB Output is correct
49 Correct 23 ms 24064 KB Output is correct
50 Correct 23 ms 24192 KB Output is correct
51 Correct 24 ms 24064 KB Output is correct
52 Correct 24 ms 23972 KB Output is correct
53 Correct 24 ms 24192 KB Output is correct
54 Correct 24 ms 24056 KB Output is correct
55 Correct 21 ms 23808 KB Output is correct
56 Correct 81 ms 30648 KB Output is correct
57 Correct 104 ms 30268 KB Output is correct
58 Correct 24 ms 24060 KB Output is correct
59 Correct 21 ms 23808 KB Output is correct
60 Correct 22 ms 23808 KB Output is correct
61 Correct 115 ms 30200 KB Output is correct
62 Correct 24 ms 24064 KB Output is correct
63 Correct 144 ms 34424 KB Output is correct
64 Correct 132 ms 30200 KB Output is correct
65 Correct 25 ms 24056 KB Output is correct
66 Correct 124 ms 30756 KB Output is correct
67 Correct 25 ms 24064 KB Output is correct
68 Correct 29 ms 24056 KB Output is correct
69 Correct 84 ms 30448 KB Output is correct
70 Correct 25 ms 24188 KB Output is correct
71 Correct 105 ms 30208 KB Output is correct
72 Correct 111 ms 31480 KB Output is correct
73 Correct 21 ms 23808 KB Output is correct
74 Correct 118 ms 34548 KB Output is correct
75 Correct 105 ms 32376 KB Output is correct
76 Correct 25 ms 24056 KB Output is correct
77 Correct 24 ms 24056 KB Output is correct
78 Correct 82 ms 29168 KB Output is correct
79 Correct 110 ms 32112 KB Output is correct
80 Correct 24 ms 24064 KB Output is correct
81 Correct 25 ms 24056 KB Output is correct
82 Correct 23 ms 23808 KB Output is correct
83 Correct 23 ms 23808 KB Output is correct
84 Correct 24 ms 24056 KB Output is correct
85 Correct 123 ms 31864 KB Output is correct
86 Correct 24 ms 24064 KB Output is correct
87 Correct 105 ms 30840 KB Output is correct
88 Correct 22 ms 23808 KB Output is correct
89 Correct 116 ms 30712 KB Output is correct
90 Correct 123 ms 31820 KB Output is correct
91 Correct 117 ms 33436 KB Output is correct
92 Correct 84 ms 30420 KB Output is correct
93 Correct 24 ms 24064 KB Output is correct
94 Correct 23 ms 23808 KB Output is correct
95 Correct 99 ms 33516 KB Output is correct
96 Correct 129 ms 35832 KB Output is correct
97 Correct 25 ms 24064 KB Output is correct
98 Correct 22 ms 23928 KB Output is correct
99 Correct 112 ms 32084 KB Output is correct
100 Correct 24 ms 24056 KB Output is correct
101 Correct 130 ms 31224 KB Output is correct
102 Correct 128 ms 34936 KB Output is correct
103 Correct 24 ms 24064 KB Output is correct
104 Correct 24 ms 24192 KB Output is correct
105 Correct 24 ms 24064 KB Output is correct
106 Correct 24 ms 24056 KB Output is correct
107 Correct 24 ms 24064 KB Output is correct
108 Correct 620 ms 60684 KB Output is correct
109 Correct 854 ms 63412 KB Output is correct
110 Correct 845 ms 69912 KB Output is correct
111 Correct 883 ms 81756 KB Output is correct
112 Correct 968 ms 75808 KB Output is correct
113 Correct 606 ms 68060 KB Output is correct
114 Correct 707 ms 52700 KB Output is correct
115 Correct 700 ms 52752 KB Output is correct
116 Correct 827 ms 57208 KB Output is correct
117 Correct 852 ms 68088 KB Output is correct
118 Correct 714 ms 54520 KB Output is correct
119 Correct 792 ms 68084 KB Output is correct
120 Correct 850 ms 76124 KB Output is correct
121 Correct 789 ms 68060 KB Output is correct
122 Correct 914 ms 61868 KB Output is correct
123 Correct 558 ms 69224 KB Output is correct
124 Correct 865 ms 66908 KB Output is correct