Submission #407584

# Submission time Handle Problem Language Result Execution time Memory
407584 2021-05-19T03:43:39 Z balbit Mergers (JOI19_mergers) C++14
100 / 100
1477 ms 160580 KB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define y1 zck_is_king
#define pii pair<int, int>
#define ull unsigned ll
#define f first
#define s second
#define ALL(x) x.begin(),x.end()
#define SZ(x) (int)x.size()
#define SQ(x) (x)*(x)
#define MN(a,b) a = min(a,(__typeof__(a))(b))
#define MX(a,b) a = max(a,(__typeof__(a))(b))
#define pb push_back
#define REP(i,n) for (int i = 0; i<n; ++i)
#define RREP(i,n) for (int i = n-1; i>=0; --i)
#define REP1(i,n) for (int i = 1; i<=n; ++i)
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#ifdef BALBIT
#define IOS()
#define bug(...) 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...);}
#else
#define IOS() ios_base::sync_with_stdio(0);cin.tie(0);
#define endl '\n'
#define bug(...)
#endif

const int iinf = 1e9+10;
const ll inf = 1ll<<60;
const ll mod = 1e9+7 ;


void GG(){cout<<"0\n"; exit(0);}

ll mpow(ll a, ll n, ll mo = mod){ // a^n % mod
    ll re=1;
    while (n>0){
        if (n&1) re = re*a %mo;
        a = a*a %mo;
        n>>=1;
    }
    return re;
}

ll inv (ll b, ll mo = mod){
    if (b==1) return b;
    return (mo-mo/b) * inv(mo%b,mo) % mo;
}

const int maxn = 5e5+5;

vector<int> g[maxn];
int dpar[maxn];
int par[maxn], dep[maxn];

void dfs(int v, int p) {
    par[v] = p;
    for (int u : g[v]) {
        if (u != p) {
            dep[u] = dep[v] + 1;
            dfs(u,v);
        }
    }
}

vector<int> where[maxn];
int find (int x ) {return x == dpar[x] ? x : dpar[x] = find(dpar[x]); }
void mrg(int a,int b ) {
    a = find(a); b = find(b);
    if (dep[a] > dep[b]) swap(a,b); // a is new root;
    dpar[b] = a;
}

set<int> adj[maxn];

signed main(){
    IOS();
    int n,k; cin>>n>>k;
    vector<pii> E;
    REP(i,n-1) {
        int a,b; cin>>a>>b; --a; --b;
        E.pb({a,b});
        g[a].pb(b); g[b].pb(a);
    }
    dfs(0, -1);
    REP(i,n) dpar[i ]= i;
    REP(i,n) {
        int x; cin>>x;
        where[x-1].pb(i);
    }
    REP(c, k) {
        int tmp = where[c][0];
        tmp = find(tmp);
        for (int i = 1; i<SZ(where[c]); ++i) {
            int v = where[c][i];
            v = find(v);
            while (v != tmp) {
                if (dep[v] < dep[tmp]) swap(v, tmp);
                // move v
                mrg(v, par[v]);
                v = find(v);
                bug(v, tmp);
            }
        }
    }
    for (pii p : E) {
        if(find (p.f) != find(p.s)) {
            adj[find(p.f)].insert(find(p.s));
            adj[find(p.s)].insert(find(p.f));
        }
    }
    int lf = 0;
    REP(i,n) {
        if (find (i) == i) {
            if (SZ(adj[i]) == 1) lf++;
        }
    }
    assert(lf != 1);
    cout<<(lf+1)/2<<endl;
}
# Verdict Execution time Memory Grader output
1 Correct 31 ms 47180 KB Output is correct
2 Correct 28 ms 47244 KB Output is correct
3 Correct 30 ms 47200 KB Output is correct
4 Correct 31 ms 47180 KB Output is correct
5 Correct 29 ms 47244 KB Output is correct
6 Correct 30 ms 47296 KB Output is correct
7 Correct 29 ms 47180 KB Output is correct
8 Correct 28 ms 47280 KB Output is correct
9 Correct 29 ms 47220 KB Output is correct
10 Correct 36 ms 47232 KB Output is correct
11 Correct 30 ms 47188 KB Output is correct
12 Correct 28 ms 47236 KB Output is correct
13 Correct 29 ms 47284 KB Output is correct
14 Correct 32 ms 47324 KB Output is correct
15 Correct 31 ms 47272 KB Output is correct
16 Correct 29 ms 47356 KB Output is correct
17 Correct 29 ms 47180 KB Output is correct
18 Correct 28 ms 47180 KB Output is correct
19 Correct 28 ms 47368 KB Output is correct
20 Correct 29 ms 47304 KB Output is correct
21 Correct 28 ms 47308 KB Output is correct
22 Correct 29 ms 47172 KB Output is correct
23 Correct 28 ms 47180 KB Output is correct
24 Correct 29 ms 47296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 31 ms 47180 KB Output is correct
2 Correct 28 ms 47244 KB Output is correct
3 Correct 30 ms 47200 KB Output is correct
4 Correct 31 ms 47180 KB Output is correct
5 Correct 29 ms 47244 KB Output is correct
6 Correct 30 ms 47296 KB Output is correct
7 Correct 29 ms 47180 KB Output is correct
8 Correct 28 ms 47280 KB Output is correct
9 Correct 29 ms 47220 KB Output is correct
10 Correct 36 ms 47232 KB Output is correct
11 Correct 30 ms 47188 KB Output is correct
12 Correct 28 ms 47236 KB Output is correct
13 Correct 29 ms 47284 KB Output is correct
14 Correct 32 ms 47324 KB Output is correct
15 Correct 31 ms 47272 KB Output is correct
16 Correct 29 ms 47356 KB Output is correct
17 Correct 29 ms 47180 KB Output is correct
18 Correct 28 ms 47180 KB Output is correct
19 Correct 28 ms 47368 KB Output is correct
20 Correct 29 ms 47304 KB Output is correct
21 Correct 28 ms 47308 KB Output is correct
22 Correct 29 ms 47172 KB Output is correct
23 Correct 28 ms 47180 KB Output is correct
24 Correct 29 ms 47296 KB Output is correct
25 Correct 29 ms 47172 KB Output is correct
26 Correct 34 ms 47680 KB Output is correct
27 Correct 30 ms 47404 KB Output is correct
28 Correct 31 ms 47692 KB Output is correct
29 Correct 34 ms 47840 KB Output is correct
30 Correct 30 ms 47464 KB Output is correct
31 Correct 28 ms 47280 KB Output is correct
32 Correct 30 ms 47668 KB Output is correct
33 Correct 30 ms 47284 KB Output is correct
34 Correct 32 ms 47436 KB Output is correct
35 Correct 32 ms 47844 KB Output is correct
36 Correct 32 ms 47428 KB Output is correct
37 Correct 31 ms 47480 KB Output is correct
38 Correct 29 ms 47248 KB Output is correct
39 Correct 30 ms 47536 KB Output is correct
40 Correct 35 ms 47412 KB Output is correct
41 Correct 30 ms 47472 KB Output is correct
42 Correct 31 ms 47460 KB Output is correct
43 Correct 35 ms 47648 KB Output is correct
44 Correct 31 ms 47328 KB Output is correct
45 Correct 31 ms 47592 KB Output is correct
46 Correct 33 ms 47436 KB Output is correct
47 Correct 35 ms 47284 KB Output is correct
48 Correct 30 ms 47456 KB Output is correct
49 Correct 31 ms 47736 KB Output is correct
50 Correct 31 ms 47812 KB Output is correct
51 Correct 30 ms 47504 KB Output is correct
52 Correct 33 ms 47484 KB Output is correct
53 Correct 30 ms 47564 KB Output is correct
54 Correct 35 ms 47400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 31 ms 47180 KB Output is correct
2 Correct 28 ms 47244 KB Output is correct
3 Correct 30 ms 47200 KB Output is correct
4 Correct 31 ms 47180 KB Output is correct
5 Correct 29 ms 47244 KB Output is correct
6 Correct 30 ms 47296 KB Output is correct
7 Correct 29 ms 47180 KB Output is correct
8 Correct 28 ms 47280 KB Output is correct
9 Correct 29 ms 47220 KB Output is correct
10 Correct 36 ms 47232 KB Output is correct
11 Correct 30 ms 47188 KB Output is correct
12 Correct 28 ms 47236 KB Output is correct
13 Correct 29 ms 47284 KB Output is correct
14 Correct 32 ms 47324 KB Output is correct
15 Correct 31 ms 47272 KB Output is correct
16 Correct 29 ms 47356 KB Output is correct
17 Correct 29 ms 47180 KB Output is correct
18 Correct 28 ms 47180 KB Output is correct
19 Correct 28 ms 47368 KB Output is correct
20 Correct 29 ms 47304 KB Output is correct
21 Correct 28 ms 47308 KB Output is correct
22 Correct 29 ms 47172 KB Output is correct
23 Correct 28 ms 47180 KB Output is correct
24 Correct 29 ms 47296 KB Output is correct
25 Correct 31 ms 47208 KB Output is correct
26 Correct 76 ms 53300 KB Output is correct
27 Correct 102 ms 53044 KB Output is correct
28 Correct 33 ms 47456 KB Output is correct
29 Correct 28 ms 47280 KB Output is correct
30 Correct 28 ms 47220 KB Output is correct
31 Correct 111 ms 52900 KB Output is correct
32 Correct 30 ms 47504 KB Output is correct
33 Correct 108 ms 58616 KB Output is correct
34 Correct 95 ms 52992 KB Output is correct
35 Correct 30 ms 47372 KB Output is correct
36 Correct 112 ms 53512 KB Output is correct
37 Correct 33 ms 47472 KB Output is correct
38 Correct 30 ms 47484 KB Output is correct
39 Correct 83 ms 53284 KB Output is correct
40 Correct 36 ms 47556 KB Output is correct
41 Correct 91 ms 53388 KB Output is correct
42 Correct 129 ms 54888 KB Output is correct
43 Correct 29 ms 47272 KB Output is correct
44 Correct 102 ms 59304 KB Output is correct
45 Correct 99 ms 55968 KB Output is correct
46 Correct 30 ms 47488 KB Output is correct
47 Correct 30 ms 47420 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 78 ms 53356 KB Output is correct
2 Correct 208 ms 65180 KB Output is correct
3 Correct 31 ms 47564 KB Output is correct
4 Correct 30 ms 47436 KB Output is correct
5 Correct 28 ms 47300 KB Output is correct
6 Correct 32 ms 47180 KB Output is correct
7 Correct 30 ms 47436 KB Output is correct
8 Correct 106 ms 54964 KB Output is correct
9 Correct 33 ms 47492 KB Output is correct
10 Correct 101 ms 53084 KB Output is correct
11 Correct 29 ms 47192 KB Output is correct
12 Correct 93 ms 53044 KB Output is correct
13 Correct 129 ms 55788 KB Output is correct
14 Correct 178 ms 64956 KB Output is correct
15 Correct 88 ms 53260 KB Output is correct
16 Correct 31 ms 47488 KB Output is correct
17 Correct 28 ms 47308 KB Output is correct
18 Correct 186 ms 63060 KB Output is correct
19 Correct 184 ms 68044 KB Output is correct
20 Correct 32 ms 47528 KB Output is correct
21 Correct 29 ms 47260 KB Output is correct
22 Correct 122 ms 56008 KB Output is correct
23 Correct 30 ms 47564 KB Output is correct
24 Correct 108 ms 54072 KB Output is correct
25 Correct 196 ms 66996 KB Output is correct
26 Correct 36 ms 47832 KB Output is correct
27 Correct 31 ms 47836 KB Output is correct
28 Correct 34 ms 47420 KB Output is correct
29 Correct 31 ms 47472 KB Output is correct
30 Correct 30 ms 47568 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 31 ms 47180 KB Output is correct
2 Correct 28 ms 47244 KB Output is correct
3 Correct 30 ms 47200 KB Output is correct
4 Correct 31 ms 47180 KB Output is correct
5 Correct 29 ms 47244 KB Output is correct
6 Correct 30 ms 47296 KB Output is correct
7 Correct 29 ms 47180 KB Output is correct
8 Correct 28 ms 47280 KB Output is correct
9 Correct 29 ms 47220 KB Output is correct
10 Correct 36 ms 47232 KB Output is correct
11 Correct 30 ms 47188 KB Output is correct
12 Correct 28 ms 47236 KB Output is correct
13 Correct 29 ms 47284 KB Output is correct
14 Correct 32 ms 47324 KB Output is correct
15 Correct 31 ms 47272 KB Output is correct
16 Correct 29 ms 47356 KB Output is correct
17 Correct 29 ms 47180 KB Output is correct
18 Correct 28 ms 47180 KB Output is correct
19 Correct 28 ms 47368 KB Output is correct
20 Correct 29 ms 47304 KB Output is correct
21 Correct 28 ms 47308 KB Output is correct
22 Correct 29 ms 47172 KB Output is correct
23 Correct 28 ms 47180 KB Output is correct
24 Correct 29 ms 47296 KB Output is correct
25 Correct 29 ms 47172 KB Output is correct
26 Correct 34 ms 47680 KB Output is correct
27 Correct 30 ms 47404 KB Output is correct
28 Correct 31 ms 47692 KB Output is correct
29 Correct 34 ms 47840 KB Output is correct
30 Correct 30 ms 47464 KB Output is correct
31 Correct 28 ms 47280 KB Output is correct
32 Correct 30 ms 47668 KB Output is correct
33 Correct 30 ms 47284 KB Output is correct
34 Correct 32 ms 47436 KB Output is correct
35 Correct 32 ms 47844 KB Output is correct
36 Correct 32 ms 47428 KB Output is correct
37 Correct 31 ms 47480 KB Output is correct
38 Correct 29 ms 47248 KB Output is correct
39 Correct 30 ms 47536 KB Output is correct
40 Correct 35 ms 47412 KB Output is correct
41 Correct 30 ms 47472 KB Output is correct
42 Correct 31 ms 47460 KB Output is correct
43 Correct 35 ms 47648 KB Output is correct
44 Correct 31 ms 47328 KB Output is correct
45 Correct 31 ms 47592 KB Output is correct
46 Correct 33 ms 47436 KB Output is correct
47 Correct 35 ms 47284 KB Output is correct
48 Correct 30 ms 47456 KB Output is correct
49 Correct 31 ms 47736 KB Output is correct
50 Correct 31 ms 47812 KB Output is correct
51 Correct 30 ms 47504 KB Output is correct
52 Correct 33 ms 47484 KB Output is correct
53 Correct 30 ms 47564 KB Output is correct
54 Correct 35 ms 47400 KB Output is correct
55 Correct 31 ms 47208 KB Output is correct
56 Correct 76 ms 53300 KB Output is correct
57 Correct 102 ms 53044 KB Output is correct
58 Correct 33 ms 47456 KB Output is correct
59 Correct 28 ms 47280 KB Output is correct
60 Correct 28 ms 47220 KB Output is correct
61 Correct 111 ms 52900 KB Output is correct
62 Correct 30 ms 47504 KB Output is correct
63 Correct 108 ms 58616 KB Output is correct
64 Correct 95 ms 52992 KB Output is correct
65 Correct 30 ms 47372 KB Output is correct
66 Correct 112 ms 53512 KB Output is correct
67 Correct 33 ms 47472 KB Output is correct
68 Correct 30 ms 47484 KB Output is correct
69 Correct 83 ms 53284 KB Output is correct
70 Correct 36 ms 47556 KB Output is correct
71 Correct 91 ms 53388 KB Output is correct
72 Correct 129 ms 54888 KB Output is correct
73 Correct 29 ms 47272 KB Output is correct
74 Correct 102 ms 59304 KB Output is correct
75 Correct 99 ms 55968 KB Output is correct
76 Correct 30 ms 47488 KB Output is correct
77 Correct 30 ms 47420 KB Output is correct
78 Correct 78 ms 53356 KB Output is correct
79 Correct 208 ms 65180 KB Output is correct
80 Correct 31 ms 47564 KB Output is correct
81 Correct 30 ms 47436 KB Output is correct
82 Correct 28 ms 47300 KB Output is correct
83 Correct 32 ms 47180 KB Output is correct
84 Correct 30 ms 47436 KB Output is correct
85 Correct 106 ms 54964 KB Output is correct
86 Correct 33 ms 47492 KB Output is correct
87 Correct 101 ms 53084 KB Output is correct
88 Correct 29 ms 47192 KB Output is correct
89 Correct 93 ms 53044 KB Output is correct
90 Correct 129 ms 55788 KB Output is correct
91 Correct 178 ms 64956 KB Output is correct
92 Correct 88 ms 53260 KB Output is correct
93 Correct 31 ms 47488 KB Output is correct
94 Correct 28 ms 47308 KB Output is correct
95 Correct 186 ms 63060 KB Output is correct
96 Correct 184 ms 68044 KB Output is correct
97 Correct 32 ms 47528 KB Output is correct
98 Correct 29 ms 47260 KB Output is correct
99 Correct 122 ms 56008 KB Output is correct
100 Correct 30 ms 47564 KB Output is correct
101 Correct 108 ms 54072 KB Output is correct
102 Correct 196 ms 66996 KB Output is correct
103 Correct 36 ms 47832 KB Output is correct
104 Correct 31 ms 47836 KB Output is correct
105 Correct 34 ms 47420 KB Output is correct
106 Correct 31 ms 47472 KB Output is correct
107 Correct 30 ms 47568 KB Output is correct
108 Correct 558 ms 95224 KB Output is correct
109 Correct 636 ms 99000 KB Output is correct
110 Correct 629 ms 108112 KB Output is correct
111 Correct 1157 ms 160580 KB Output is correct
112 Correct 1107 ms 134476 KB Output is correct
113 Correct 1350 ms 136564 KB Output is correct
114 Correct 551 ms 84120 KB Output is correct
115 Correct 577 ms 84816 KB Output is correct
116 Correct 669 ms 87660 KB Output is correct
117 Correct 1153 ms 145816 KB Output is correct
118 Correct 573 ms 84216 KB Output is correct
119 Correct 1190 ms 145744 KB Output is correct
120 Correct 1169 ms 156864 KB Output is correct
121 Correct 1143 ms 145684 KB Output is correct
122 Correct 782 ms 100648 KB Output is correct
123 Correct 1477 ms 147296 KB Output is correct
124 Correct 984 ms 127596 KB Output is correct