답안 #210305

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
210305 2020-03-17T02:49:44 Z ToMmyDong Mergers (JOI19_mergers) C++11
100 / 100
2155 ms 164824 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define REP(i,n) for(int i=0;i<n;++i)
#define REP1(i,n) for(int i=1;i<=n;++i)
#define SZ(i) int(i.size())
#define eb emplace_back
#define ALL(i) i.begin(),i.end()
#define X first
#define Y second
#ifdef tmd
#define IOS()
#define debug(...) fprintf(stderr,"#%d: %s = ",__LINE__,#__VA_ARGS__),_do(__VA_ARGS__);
template<typename T> void _do(T &&x){cerr<<x<<endl;}
template<typename T, typename ...S> void _do(T &&x, S &&...y){cerr<<x<<", ";_do(y...);}
template<typename It> ostream& _printRng(ostream &os,It bg,It ed)
{
    os<<"{";
    for(It it=bg;it!=ed;it++) {
        os<<(it==bg?"":",")<<*it;
    }
    os<<"}";
    return os;
}
template<typename T> ostream &operator << (ostream &os,vector<T> &v){return _printRng(os,v.begin(), v.end());}
template<typename T> void pary(T bg, T ed){_printRng(cerr,bg,ed);cerr<<endl;}
#else
#define IOS() ios_base::sync_with_stdio(0);cin.tie(0);
#define endl '\n'
#define debug(...)
#define pary(...)
#endif

const int MAXN = 500005;
const ll MOD = 1000000007;

int n, k;
vector<pii> edges;
vector<int> G[MAXN], T[MAXN];

vector<int> tree, comp;

int s[MAXN], cnt[MAXN];
map<int,int> cur[MAXN];
set<int> open[MAXN];

void add (int f, int t) { //
    if (cur[f].size() > cur[t].size()) {
        swap(cur[f], cur[t]);
        swap(open[f], open[t]);
    }

    for (auto v : open[f]) {
        open[t].insert(v);
    }
    for (auto p : cur[f]) {
        cur[t][p.X] += p.Y;
        if (cur[t][p.X] == cnt[p.X]) {
            open[t].erase(p.X);
        }
    }

    open[f].clear();
    cur[f].clear();
}

void dfsEdg(int nd, int pa, int id) {

    cur[nd][s[nd]]++;
    if (cnt[s[nd]] != 1) {
        open[nd].insert(s[nd]);
    }

    for (auto eid : G[nd]) {
        int v = edges[eid].X ^ edges[eid].Y ^ nd;
        if (v != pa) {
            dfsEdg(v, nd, eid);
            add(v, nd);
        }
    }

    if (id == -1) {
        return;
    }

    if (open[nd].empty()) {
        tree.eb(id);
    } else {
        comp.eb(id);
    }
}

int djs[MAXN];

int fnd (int nd) {
    return nd == djs[nd] ? djs[nd] : djs[nd] = fnd(djs[nd]);
}

void mrg (int x, int y) {
    x = fnd(x);
    y = fnd(y);
    djs[x] = y;
}

int deg[MAXN];
/*********************GoodLuck***********************/
int main () {
    IOS();

    cin >> n >> k;
    REP (i, n-1) {
        int u,v;
        cin >> u >> v;
        edges.eb(u, v);
        G[u].eb(i);
        G[v].eb(i);
    }

    REP1 (i, n) {
        cin >> s[i];
        cnt[s[i]]++;
    }


    dfsEdg(1, 1, -1);

    for (int i=1; i<=n; i++) {
        djs[i] = i;
    }

    for (auto id : comp) {
        mrg(edges[id].X, edges[id].Y);
    }
    vector<int> nodes;
    for (int i=1; i<=n; i++) {
        if (fnd(i) == i) {
            nodes.eb(i);
        }
    }

    debug(tree, comp, nodes);
    for (auto id : tree) {
        int u, v;
        tie(u, v) = edges[id];

        deg[fnd(u)]++;
        deg[fnd(v)]++;
    }

    int ans = 0;
    for (auto v : nodes) {
        ans += (deg[v] == 1);
    }

    if (ans == 1) {
        cout << 0 << endl;
    } else {
        cout << (ans+1)/2 << endl;
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 48 ms 70776 KB Output is correct
2 Correct 47 ms 70776 KB Output is correct
3 Correct 47 ms 70776 KB Output is correct
4 Correct 47 ms 70776 KB Output is correct
5 Correct 46 ms 70776 KB Output is correct
6 Correct 47 ms 70776 KB Output is correct
7 Correct 48 ms 70776 KB Output is correct
8 Correct 46 ms 70776 KB Output is correct
9 Correct 48 ms 70776 KB Output is correct
10 Correct 48 ms 70868 KB Output is correct
11 Correct 49 ms 70776 KB Output is correct
12 Correct 47 ms 70776 KB Output is correct
13 Correct 47 ms 70776 KB Output is correct
14 Correct 46 ms 70776 KB Output is correct
15 Correct 48 ms 70776 KB Output is correct
16 Correct 47 ms 70776 KB Output is correct
17 Correct 47 ms 70776 KB Output is correct
18 Correct 48 ms 70776 KB Output is correct
19 Correct 48 ms 70776 KB Output is correct
20 Correct 48 ms 70776 KB Output is correct
21 Correct 47 ms 70776 KB Output is correct
22 Correct 48 ms 70776 KB Output is correct
23 Correct 48 ms 70776 KB Output is correct
24 Correct 47 ms 70776 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 48 ms 70776 KB Output is correct
2 Correct 47 ms 70776 KB Output is correct
3 Correct 47 ms 70776 KB Output is correct
4 Correct 47 ms 70776 KB Output is correct
5 Correct 46 ms 70776 KB Output is correct
6 Correct 47 ms 70776 KB Output is correct
7 Correct 48 ms 70776 KB Output is correct
8 Correct 46 ms 70776 KB Output is correct
9 Correct 48 ms 70776 KB Output is correct
10 Correct 48 ms 70868 KB Output is correct
11 Correct 49 ms 70776 KB Output is correct
12 Correct 47 ms 70776 KB Output is correct
13 Correct 47 ms 70776 KB Output is correct
14 Correct 46 ms 70776 KB Output is correct
15 Correct 48 ms 70776 KB Output is correct
16 Correct 47 ms 70776 KB Output is correct
17 Correct 47 ms 70776 KB Output is correct
18 Correct 48 ms 70776 KB Output is correct
19 Correct 48 ms 70776 KB Output is correct
20 Correct 48 ms 70776 KB Output is correct
21 Correct 47 ms 70776 KB Output is correct
22 Correct 48 ms 70776 KB Output is correct
23 Correct 48 ms 70776 KB Output is correct
24 Correct 47 ms 70776 KB Output is correct
25 Correct 49 ms 70776 KB Output is correct
26 Correct 53 ms 71160 KB Output is correct
27 Correct 51 ms 71100 KB Output is correct
28 Correct 51 ms 71288 KB Output is correct
29 Correct 51 ms 71288 KB Output is correct
30 Correct 52 ms 71032 KB Output is correct
31 Correct 47 ms 70776 KB Output is correct
32 Correct 51 ms 71416 KB Output is correct
33 Correct 48 ms 70776 KB Output is correct
34 Correct 49 ms 71032 KB Output is correct
35 Correct 53 ms 71288 KB Output is correct
36 Correct 50 ms 71032 KB Output is correct
37 Correct 52 ms 71160 KB Output is correct
38 Correct 47 ms 70780 KB Output is correct
39 Correct 52 ms 71288 KB Output is correct
40 Correct 49 ms 71032 KB Output is correct
41 Correct 49 ms 71032 KB Output is correct
42 Correct 51 ms 71160 KB Output is correct
43 Correct 51 ms 71544 KB Output is correct
44 Correct 48 ms 70776 KB Output is correct
45 Correct 51 ms 71288 KB Output is correct
46 Correct 52 ms 71160 KB Output is correct
47 Correct 47 ms 70780 KB Output is correct
48 Correct 49 ms 71032 KB Output is correct
49 Correct 51 ms 71288 KB Output is correct
50 Correct 51 ms 71288 KB Output is correct
51 Correct 50 ms 71032 KB Output is correct
52 Correct 50 ms 71032 KB Output is correct
53 Correct 50 ms 71160 KB Output is correct
54 Correct 51 ms 71032 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 48 ms 70776 KB Output is correct
2 Correct 47 ms 70776 KB Output is correct
3 Correct 47 ms 70776 KB Output is correct
4 Correct 47 ms 70776 KB Output is correct
5 Correct 46 ms 70776 KB Output is correct
6 Correct 47 ms 70776 KB Output is correct
7 Correct 48 ms 70776 KB Output is correct
8 Correct 46 ms 70776 KB Output is correct
9 Correct 48 ms 70776 KB Output is correct
10 Correct 48 ms 70868 KB Output is correct
11 Correct 49 ms 70776 KB Output is correct
12 Correct 47 ms 70776 KB Output is correct
13 Correct 47 ms 70776 KB Output is correct
14 Correct 46 ms 70776 KB Output is correct
15 Correct 48 ms 70776 KB Output is correct
16 Correct 47 ms 70776 KB Output is correct
17 Correct 47 ms 70776 KB Output is correct
18 Correct 48 ms 70776 KB Output is correct
19 Correct 48 ms 70776 KB Output is correct
20 Correct 48 ms 70776 KB Output is correct
21 Correct 47 ms 70776 KB Output is correct
22 Correct 48 ms 70776 KB Output is correct
23 Correct 48 ms 70776 KB Output is correct
24 Correct 47 ms 70776 KB Output is correct
25 Correct 48 ms 70776 KB Output is correct
26 Correct 136 ms 78056 KB Output is correct
27 Correct 207 ms 77608 KB Output is correct
28 Correct 51 ms 71032 KB Output is correct
29 Correct 52 ms 70776 KB Output is correct
30 Correct 48 ms 70776 KB Output is correct
31 Correct 153 ms 77720 KB Output is correct
32 Correct 50 ms 71032 KB Output is correct
33 Correct 210 ms 94056 KB Output is correct
34 Correct 198 ms 77544 KB Output is correct
35 Correct 50 ms 71032 KB Output is correct
36 Correct 173 ms 79208 KB Output is correct
37 Correct 48 ms 71032 KB Output is correct
38 Correct 50 ms 71032 KB Output is correct
39 Correct 138 ms 78184 KB Output is correct
40 Correct 50 ms 71544 KB Output is correct
41 Correct 164 ms 77544 KB Output is correct
42 Correct 189 ms 82664 KB Output is correct
43 Correct 47 ms 70776 KB Output is correct
44 Correct 198 ms 94316 KB Output is correct
45 Correct 205 ms 88040 KB Output is correct
46 Correct 50 ms 71032 KB Output is correct
47 Correct 52 ms 71032 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 139 ms 77416 KB Output is correct
2 Correct 199 ms 83124 KB Output is correct
3 Correct 52 ms 71160 KB Output is correct
4 Correct 49 ms 71032 KB Output is correct
5 Correct 48 ms 70776 KB Output is correct
6 Correct 47 ms 70776 KB Output is correct
7 Correct 50 ms 71032 KB Output is correct
8 Correct 267 ms 80368 KB Output is correct
9 Correct 49 ms 71032 KB Output is correct
10 Correct 162 ms 77928 KB Output is correct
11 Correct 47 ms 70776 KB Output is correct
12 Correct 174 ms 77932 KB Output is correct
13 Correct 245 ms 80488 KB Output is correct
14 Correct 286 ms 84204 KB Output is correct
15 Correct 135 ms 78188 KB Output is correct
16 Correct 52 ms 71148 KB Output is correct
17 Correct 46 ms 70776 KB Output is correct
18 Correct 207 ms 83624 KB Output is correct
19 Correct 248 ms 89576 KB Output is correct
20 Correct 52 ms 71160 KB Output is correct
21 Correct 49 ms 70852 KB Output is correct
22 Correct 216 ms 81516 KB Output is correct
23 Correct 51 ms 71032 KB Output is correct
24 Correct 173 ms 78060 KB Output is correct
25 Correct 239 ms 87144 KB Output is correct
26 Correct 52 ms 71288 KB Output is correct
27 Correct 52 ms 71288 KB Output is correct
28 Correct 51 ms 71032 KB Output is correct
29 Correct 49 ms 71032 KB Output is correct
30 Correct 51 ms 71160 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 48 ms 70776 KB Output is correct
2 Correct 47 ms 70776 KB Output is correct
3 Correct 47 ms 70776 KB Output is correct
4 Correct 47 ms 70776 KB Output is correct
5 Correct 46 ms 70776 KB Output is correct
6 Correct 47 ms 70776 KB Output is correct
7 Correct 48 ms 70776 KB Output is correct
8 Correct 46 ms 70776 KB Output is correct
9 Correct 48 ms 70776 KB Output is correct
10 Correct 48 ms 70868 KB Output is correct
11 Correct 49 ms 70776 KB Output is correct
12 Correct 47 ms 70776 KB Output is correct
13 Correct 47 ms 70776 KB Output is correct
14 Correct 46 ms 70776 KB Output is correct
15 Correct 48 ms 70776 KB Output is correct
16 Correct 47 ms 70776 KB Output is correct
17 Correct 47 ms 70776 KB Output is correct
18 Correct 48 ms 70776 KB Output is correct
19 Correct 48 ms 70776 KB Output is correct
20 Correct 48 ms 70776 KB Output is correct
21 Correct 47 ms 70776 KB Output is correct
22 Correct 48 ms 70776 KB Output is correct
23 Correct 48 ms 70776 KB Output is correct
24 Correct 47 ms 70776 KB Output is correct
25 Correct 49 ms 70776 KB Output is correct
26 Correct 53 ms 71160 KB Output is correct
27 Correct 51 ms 71100 KB Output is correct
28 Correct 51 ms 71288 KB Output is correct
29 Correct 51 ms 71288 KB Output is correct
30 Correct 52 ms 71032 KB Output is correct
31 Correct 47 ms 70776 KB Output is correct
32 Correct 51 ms 71416 KB Output is correct
33 Correct 48 ms 70776 KB Output is correct
34 Correct 49 ms 71032 KB Output is correct
35 Correct 53 ms 71288 KB Output is correct
36 Correct 50 ms 71032 KB Output is correct
37 Correct 52 ms 71160 KB Output is correct
38 Correct 47 ms 70780 KB Output is correct
39 Correct 52 ms 71288 KB Output is correct
40 Correct 49 ms 71032 KB Output is correct
41 Correct 49 ms 71032 KB Output is correct
42 Correct 51 ms 71160 KB Output is correct
43 Correct 51 ms 71544 KB Output is correct
44 Correct 48 ms 70776 KB Output is correct
45 Correct 51 ms 71288 KB Output is correct
46 Correct 52 ms 71160 KB Output is correct
47 Correct 47 ms 70780 KB Output is correct
48 Correct 49 ms 71032 KB Output is correct
49 Correct 51 ms 71288 KB Output is correct
50 Correct 51 ms 71288 KB Output is correct
51 Correct 50 ms 71032 KB Output is correct
52 Correct 50 ms 71032 KB Output is correct
53 Correct 50 ms 71160 KB Output is correct
54 Correct 51 ms 71032 KB Output is correct
55 Correct 48 ms 70776 KB Output is correct
56 Correct 136 ms 78056 KB Output is correct
57 Correct 207 ms 77608 KB Output is correct
58 Correct 51 ms 71032 KB Output is correct
59 Correct 52 ms 70776 KB Output is correct
60 Correct 48 ms 70776 KB Output is correct
61 Correct 153 ms 77720 KB Output is correct
62 Correct 50 ms 71032 KB Output is correct
63 Correct 210 ms 94056 KB Output is correct
64 Correct 198 ms 77544 KB Output is correct
65 Correct 50 ms 71032 KB Output is correct
66 Correct 173 ms 79208 KB Output is correct
67 Correct 48 ms 71032 KB Output is correct
68 Correct 50 ms 71032 KB Output is correct
69 Correct 138 ms 78184 KB Output is correct
70 Correct 50 ms 71544 KB Output is correct
71 Correct 164 ms 77544 KB Output is correct
72 Correct 189 ms 82664 KB Output is correct
73 Correct 47 ms 70776 KB Output is correct
74 Correct 198 ms 94316 KB Output is correct
75 Correct 205 ms 88040 KB Output is correct
76 Correct 50 ms 71032 KB Output is correct
77 Correct 52 ms 71032 KB Output is correct
78 Correct 139 ms 77416 KB Output is correct
79 Correct 199 ms 83124 KB Output is correct
80 Correct 52 ms 71160 KB Output is correct
81 Correct 49 ms 71032 KB Output is correct
82 Correct 48 ms 70776 KB Output is correct
83 Correct 47 ms 70776 KB Output is correct
84 Correct 50 ms 71032 KB Output is correct
85 Correct 267 ms 80368 KB Output is correct
86 Correct 49 ms 71032 KB Output is correct
87 Correct 162 ms 77928 KB Output is correct
88 Correct 47 ms 70776 KB Output is correct
89 Correct 174 ms 77932 KB Output is correct
90 Correct 245 ms 80488 KB Output is correct
91 Correct 286 ms 84204 KB Output is correct
92 Correct 135 ms 78188 KB Output is correct
93 Correct 52 ms 71148 KB Output is correct
94 Correct 46 ms 70776 KB Output is correct
95 Correct 207 ms 83624 KB Output is correct
96 Correct 248 ms 89576 KB Output is correct
97 Correct 52 ms 71160 KB Output is correct
98 Correct 49 ms 70852 KB Output is correct
99 Correct 216 ms 81516 KB Output is correct
100 Correct 51 ms 71032 KB Output is correct
101 Correct 173 ms 78060 KB Output is correct
102 Correct 239 ms 87144 KB Output is correct
103 Correct 52 ms 71288 KB Output is correct
104 Correct 52 ms 71288 KB Output is correct
105 Correct 51 ms 71032 KB Output is correct
106 Correct 49 ms 71032 KB Output is correct
107 Correct 51 ms 71160 KB Output is correct
108 Correct 1332 ms 116908 KB Output is correct
109 Correct 1098 ms 138124 KB Output is correct
110 Correct 1057 ms 164824 KB Output is correct
111 Correct 1604 ms 161884 KB Output is correct
112 Correct 1519 ms 147676 KB Output is correct
113 Correct 1401 ms 127964 KB Output is correct
114 Correct 853 ms 99420 KB Output is correct
115 Correct 834 ms 99420 KB Output is correct
116 Correct 1537 ms 114908 KB Output is correct
117 Correct 1658 ms 140640 KB Output is correct
118 Correct 820 ms 108124 KB Output is correct
119 Correct 1652 ms 139872 KB Output is correct
120 Correct 1501 ms 157020 KB Output is correct
121 Correct 1644 ms 138844 KB Output is correct
122 Correct 2155 ms 133596 KB Output is correct
123 Correct 1172 ms 139612 KB Output is correct
124 Correct 1510 ms 137820 KB Output is correct