답안 #293521

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
293521 2020-09-08T07:14:26 Z Aldas25 Mergers (JOI19_mergers) C++14
100 / 100
1768 ms 165392 KB
//#pragma GCC optimize("O3")

#include <bits/stdc++.h>
//#include <ctime>

using namespace std;

#define FAST_IO ios_base::sync_with_stdio(0); cin.tie(nullptr)
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define REP(n) FOR(O, 1, (n))
#define pb push_back
#define f first
#define s second
typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef vector<ll> vl;
typedef vector<piii> viii;
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int MAXN = 500100, MAXK = 20;
const ll MOD = 1e9+7;
const ll INF = 1e16;
const ld PI = asin(1) * 2;

void setIO () {
    FAST_IO;
}

void setIO (string s) {
    setIO();
 	freopen((s+".in").c_str(),"r",stdin);
 	freopen((s+".out").c_str(),"w",stdout);
}

int n, k;
vi adj[MAXN];
int s[MAXN];
vi bel[MAXN];

int deg[MAXN];

int par[MAXN];
int find (int a) {return par[a] = par[a]==a ? a : find(par[a]);}
bool same (int a, int b) {return find(a) == find(b);}
void unite (int a, int b) {
    a = find(a), b = find(b);
    if (a == b) return;
    par[b] = a;
}

int curT = 0;
vi times[MAXN];
int pr[MAXN][MAXK];
int dep[MAXN];

void dfs1 (int v, int p) {
    times[v].pb(++curT);

    FOR(i, 1,MAXK-1)
        pr[v][i] = pr[pr[v][i-1]][i-1];

    for (int u : adj[v]) {
        if (u == p) continue;
        pr[u][0] = v;
        dep[u] = dep[v]+1;
        dfs1 (u, v);
        times[v].pb(++curT);
    }
}

int lca (int v, int u) {
    if (u == v) return u;
    if (dep[u] > dep[v]) swap(u,v);
    int d = dep[v] - dep[u];
    FOR(i, 0, MAXK-1)
        if (d & (1<<i))
            v = pr[v][i];
    if (u == v) return u;
    for (int i = MAXK-1; i >= 0; i--) {
        if (pr[v][i] != pr[u][i]) {
            v = pr[v][i];
            u = pr[u][i];
        }
    }
    return pr[v][0];
}

int cnt[MAXN];

void dfs3 (int v, int p) {
    for (int u : adj[v]) {
        if (u == p) continue;
        dfs3(u,v);
        cnt[v] += cnt[u];
    }
//    cout << "  v = " << v << " cnt = " << cnt[v] << endl;

    if (p != -1 && cnt[v] > 0) unite(v,p);
}

void dfs2 (int v, int p) {
    for (int u : adj[v]) {
        if (u == p) continue;
        dfs2(u, v);
    }
    if (p == -1) return;
    if (!same(v,p)) {
        deg[find(v)]++;
        deg[find(p)]++;
    }
}

vii seq;

int main() {
    setIO();

   // ifstream cin ("in.txt");

    cin >> n >> k;
    REP(n-1) {
        int a, b; cin >> a >> b;
        adj[a].pb(b);
        adj[b].pb(a);
    }
    FOR(i, 1, n) cin >> s[i];
    FOR(i,1 , n) bel[s[i]].pb(i);

    FOR(i, 1, n) par[i] = i;
    dfs1 (1, -1);


    FOR(i, 1, k) {
        seq.clear();
        for (int v : bel[i])
            for (int t : times[v])
                seq.pb({t, v});
        sort(seq.begin(), seq.end());
        FOR(j, 0, (int)seq.size()-2) {
            int u = seq[j].s;
            int v = seq[j+1].s;
            cnt[u]++;
            cnt[v]++;
            cnt[lca(u,v)] -= 2;
        }
    }

    dfs3 (1, -1);

    dfs2 (1, -1);

    //FOR(i, 1, n) cout <<"   i = " << i << " deg = " << deg[i] << endl;
    //FOR(i, 1, n) cout << "  i = " << i << " par = " << par[i] << endl;

    int leaves = 0;
    FOR(i, 1, n) if (deg[i] == 1) leaves++;
    int ans = (leaves+1)/2;
    cout << ans <<"\n";

	return 0;
}

Compilation message

mergers.cpp: In function 'void setIO(std::string)':
mergers.cpp:35:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   35 |   freopen((s+".in").c_str(),"r",stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mergers.cpp:36:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   36 |   freopen((s+".out").c_str(),"w",stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 35576 KB Output is correct
2 Correct 27 ms 35584 KB Output is correct
3 Correct 25 ms 35584 KB Output is correct
4 Correct 24 ms 35584 KB Output is correct
5 Correct 25 ms 35584 KB Output is correct
6 Correct 24 ms 35576 KB Output is correct
7 Correct 24 ms 35584 KB Output is correct
8 Correct 24 ms 35584 KB Output is correct
9 Correct 24 ms 35584 KB Output is correct
10 Correct 24 ms 35584 KB Output is correct
11 Correct 26 ms 35584 KB Output is correct
12 Correct 25 ms 35584 KB Output is correct
13 Correct 25 ms 35584 KB Output is correct
14 Correct 24 ms 35584 KB Output is correct
15 Correct 25 ms 35584 KB Output is correct
16 Correct 24 ms 35584 KB Output is correct
17 Correct 26 ms 35584 KB Output is correct
18 Correct 24 ms 35584 KB Output is correct
19 Correct 24 ms 35584 KB Output is correct
20 Correct 25 ms 35712 KB Output is correct
21 Correct 24 ms 35584 KB Output is correct
22 Correct 25 ms 35712 KB Output is correct
23 Correct 25 ms 35584 KB Output is correct
24 Correct 24 ms 35584 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 35576 KB Output is correct
2 Correct 27 ms 35584 KB Output is correct
3 Correct 25 ms 35584 KB Output is correct
4 Correct 24 ms 35584 KB Output is correct
5 Correct 25 ms 35584 KB Output is correct
6 Correct 24 ms 35576 KB Output is correct
7 Correct 24 ms 35584 KB Output is correct
8 Correct 24 ms 35584 KB Output is correct
9 Correct 24 ms 35584 KB Output is correct
10 Correct 24 ms 35584 KB Output is correct
11 Correct 26 ms 35584 KB Output is correct
12 Correct 25 ms 35584 KB Output is correct
13 Correct 25 ms 35584 KB Output is correct
14 Correct 24 ms 35584 KB Output is correct
15 Correct 25 ms 35584 KB Output is correct
16 Correct 24 ms 35584 KB Output is correct
17 Correct 26 ms 35584 KB Output is correct
18 Correct 24 ms 35584 KB Output is correct
19 Correct 24 ms 35584 KB Output is correct
20 Correct 25 ms 35712 KB Output is correct
21 Correct 24 ms 35584 KB Output is correct
22 Correct 25 ms 35712 KB Output is correct
23 Correct 25 ms 35584 KB Output is correct
24 Correct 24 ms 35584 KB Output is correct
25 Correct 24 ms 35584 KB Output is correct
26 Correct 31 ms 36320 KB Output is correct
27 Correct 27 ms 36096 KB Output is correct
28 Correct 28 ms 36344 KB Output is correct
29 Correct 27 ms 36224 KB Output is correct
30 Correct 27 ms 36088 KB Output is correct
31 Correct 24 ms 35584 KB Output is correct
32 Correct 27 ms 36344 KB Output is correct
33 Correct 24 ms 35584 KB Output is correct
34 Correct 27 ms 36216 KB Output is correct
35 Correct 28 ms 36224 KB Output is correct
36 Correct 28 ms 36096 KB Output is correct
37 Correct 27 ms 36216 KB Output is correct
38 Correct 25 ms 35584 KB Output is correct
39 Correct 28 ms 36224 KB Output is correct
40 Correct 28 ms 36352 KB Output is correct
41 Correct 27 ms 36224 KB Output is correct
42 Correct 27 ms 36216 KB Output is correct
43 Correct 31 ms 36480 KB Output is correct
44 Correct 25 ms 35584 KB Output is correct
45 Correct 28 ms 36216 KB Output is correct
46 Correct 28 ms 36224 KB Output is correct
47 Correct 24 ms 35584 KB Output is correct
48 Correct 28 ms 36096 KB Output is correct
49 Correct 27 ms 36352 KB Output is correct
50 Correct 28 ms 36216 KB Output is correct
51 Correct 27 ms 36096 KB Output is correct
52 Correct 27 ms 36216 KB Output is correct
53 Correct 27 ms 36480 KB Output is correct
54 Correct 29 ms 36096 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 35576 KB Output is correct
2 Correct 27 ms 35584 KB Output is correct
3 Correct 25 ms 35584 KB Output is correct
4 Correct 24 ms 35584 KB Output is correct
5 Correct 25 ms 35584 KB Output is correct
6 Correct 24 ms 35576 KB Output is correct
7 Correct 24 ms 35584 KB Output is correct
8 Correct 24 ms 35584 KB Output is correct
9 Correct 24 ms 35584 KB Output is correct
10 Correct 24 ms 35584 KB Output is correct
11 Correct 26 ms 35584 KB Output is correct
12 Correct 25 ms 35584 KB Output is correct
13 Correct 25 ms 35584 KB Output is correct
14 Correct 24 ms 35584 KB Output is correct
15 Correct 25 ms 35584 KB Output is correct
16 Correct 24 ms 35584 KB Output is correct
17 Correct 26 ms 35584 KB Output is correct
18 Correct 24 ms 35584 KB Output is correct
19 Correct 24 ms 35584 KB Output is correct
20 Correct 25 ms 35712 KB Output is correct
21 Correct 24 ms 35584 KB Output is correct
22 Correct 25 ms 35712 KB Output is correct
23 Correct 25 ms 35584 KB Output is correct
24 Correct 24 ms 35584 KB Output is correct
25 Correct 29 ms 35584 KB Output is correct
26 Correct 156 ms 56428 KB Output is correct
27 Correct 231 ms 53496 KB Output is correct
28 Correct 28 ms 36096 KB Output is correct
29 Correct 24 ms 35584 KB Output is correct
30 Correct 25 ms 35584 KB Output is correct
31 Correct 223 ms 53500 KB Output is correct
32 Correct 27 ms 36216 KB Output is correct
33 Correct 281 ms 60536 KB Output is correct
34 Correct 218 ms 53496 KB Output is correct
35 Correct 27 ms 36224 KB Output is correct
36 Correct 229 ms 54136 KB Output is correct
37 Correct 28 ms 36216 KB Output is correct
38 Correct 27 ms 36216 KB Output is correct
39 Correct 158 ms 55276 KB Output is correct
40 Correct 28 ms 36480 KB Output is correct
41 Correct 215 ms 55016 KB Output is correct
42 Correct 279 ms 55672 KB Output is correct
43 Correct 25 ms 35584 KB Output is correct
44 Correct 264 ms 62432 KB Output is correct
45 Correct 285 ms 57208 KB Output is correct
46 Correct 27 ms 36352 KB Output is correct
47 Correct 29 ms 36088 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 170 ms 55016 KB Output is correct
2 Correct 141 ms 57836 KB Output is correct
3 Correct 27 ms 36224 KB Output is correct
4 Correct 28 ms 36224 KB Output is correct
5 Correct 25 ms 35576 KB Output is correct
6 Correct 24 ms 35584 KB Output is correct
7 Correct 31 ms 36216 KB Output is correct
8 Correct 240 ms 55288 KB Output is correct
9 Correct 28 ms 36092 KB Output is correct
10 Correct 217 ms 54036 KB Output is correct
11 Correct 25 ms 35584 KB Output is correct
12 Correct 232 ms 54008 KB Output is correct
13 Correct 227 ms 55032 KB Output is correct
14 Correct 203 ms 56824 KB Output is correct
15 Correct 163 ms 55144 KB Output is correct
16 Correct 29 ms 36224 KB Output is correct
17 Correct 24 ms 35584 KB Output is correct
18 Correct 148 ms 57836 KB Output is correct
19 Correct 266 ms 60664 KB Output is correct
20 Correct 25 ms 36216 KB Output is correct
21 Correct 22 ms 35584 KB Output is correct
22 Correct 162 ms 57836 KB Output is correct
23 Correct 28 ms 36224 KB Output is correct
24 Correct 229 ms 54184 KB Output is correct
25 Correct 264 ms 59256 KB Output is correct
26 Correct 27 ms 36352 KB Output is correct
27 Correct 30 ms 36224 KB Output is correct
28 Correct 27 ms 36088 KB Output is correct
29 Correct 28 ms 36224 KB Output is correct
30 Correct 27 ms 36352 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 35576 KB Output is correct
2 Correct 27 ms 35584 KB Output is correct
3 Correct 25 ms 35584 KB Output is correct
4 Correct 24 ms 35584 KB Output is correct
5 Correct 25 ms 35584 KB Output is correct
6 Correct 24 ms 35576 KB Output is correct
7 Correct 24 ms 35584 KB Output is correct
8 Correct 24 ms 35584 KB Output is correct
9 Correct 24 ms 35584 KB Output is correct
10 Correct 24 ms 35584 KB Output is correct
11 Correct 26 ms 35584 KB Output is correct
12 Correct 25 ms 35584 KB Output is correct
13 Correct 25 ms 35584 KB Output is correct
14 Correct 24 ms 35584 KB Output is correct
15 Correct 25 ms 35584 KB Output is correct
16 Correct 24 ms 35584 KB Output is correct
17 Correct 26 ms 35584 KB Output is correct
18 Correct 24 ms 35584 KB Output is correct
19 Correct 24 ms 35584 KB Output is correct
20 Correct 25 ms 35712 KB Output is correct
21 Correct 24 ms 35584 KB Output is correct
22 Correct 25 ms 35712 KB Output is correct
23 Correct 25 ms 35584 KB Output is correct
24 Correct 24 ms 35584 KB Output is correct
25 Correct 24 ms 35584 KB Output is correct
26 Correct 31 ms 36320 KB Output is correct
27 Correct 27 ms 36096 KB Output is correct
28 Correct 28 ms 36344 KB Output is correct
29 Correct 27 ms 36224 KB Output is correct
30 Correct 27 ms 36088 KB Output is correct
31 Correct 24 ms 35584 KB Output is correct
32 Correct 27 ms 36344 KB Output is correct
33 Correct 24 ms 35584 KB Output is correct
34 Correct 27 ms 36216 KB Output is correct
35 Correct 28 ms 36224 KB Output is correct
36 Correct 28 ms 36096 KB Output is correct
37 Correct 27 ms 36216 KB Output is correct
38 Correct 25 ms 35584 KB Output is correct
39 Correct 28 ms 36224 KB Output is correct
40 Correct 28 ms 36352 KB Output is correct
41 Correct 27 ms 36224 KB Output is correct
42 Correct 27 ms 36216 KB Output is correct
43 Correct 31 ms 36480 KB Output is correct
44 Correct 25 ms 35584 KB Output is correct
45 Correct 28 ms 36216 KB Output is correct
46 Correct 28 ms 36224 KB Output is correct
47 Correct 24 ms 35584 KB Output is correct
48 Correct 28 ms 36096 KB Output is correct
49 Correct 27 ms 36352 KB Output is correct
50 Correct 28 ms 36216 KB Output is correct
51 Correct 27 ms 36096 KB Output is correct
52 Correct 27 ms 36216 KB Output is correct
53 Correct 27 ms 36480 KB Output is correct
54 Correct 29 ms 36096 KB Output is correct
55 Correct 29 ms 35584 KB Output is correct
56 Correct 156 ms 56428 KB Output is correct
57 Correct 231 ms 53496 KB Output is correct
58 Correct 28 ms 36096 KB Output is correct
59 Correct 24 ms 35584 KB Output is correct
60 Correct 25 ms 35584 KB Output is correct
61 Correct 223 ms 53500 KB Output is correct
62 Correct 27 ms 36216 KB Output is correct
63 Correct 281 ms 60536 KB Output is correct
64 Correct 218 ms 53496 KB Output is correct
65 Correct 27 ms 36224 KB Output is correct
66 Correct 229 ms 54136 KB Output is correct
67 Correct 28 ms 36216 KB Output is correct
68 Correct 27 ms 36216 KB Output is correct
69 Correct 158 ms 55276 KB Output is correct
70 Correct 28 ms 36480 KB Output is correct
71 Correct 215 ms 55016 KB Output is correct
72 Correct 279 ms 55672 KB Output is correct
73 Correct 25 ms 35584 KB Output is correct
74 Correct 264 ms 62432 KB Output is correct
75 Correct 285 ms 57208 KB Output is correct
76 Correct 27 ms 36352 KB Output is correct
77 Correct 29 ms 36088 KB Output is correct
78 Correct 170 ms 55016 KB Output is correct
79 Correct 141 ms 57836 KB Output is correct
80 Correct 27 ms 36224 KB Output is correct
81 Correct 28 ms 36224 KB Output is correct
82 Correct 25 ms 35576 KB Output is correct
83 Correct 24 ms 35584 KB Output is correct
84 Correct 31 ms 36216 KB Output is correct
85 Correct 240 ms 55288 KB Output is correct
86 Correct 28 ms 36092 KB Output is correct
87 Correct 217 ms 54036 KB Output is correct
88 Correct 25 ms 35584 KB Output is correct
89 Correct 232 ms 54008 KB Output is correct
90 Correct 227 ms 55032 KB Output is correct
91 Correct 203 ms 56824 KB Output is correct
92 Correct 163 ms 55144 KB Output is correct
93 Correct 29 ms 36224 KB Output is correct
94 Correct 24 ms 35584 KB Output is correct
95 Correct 148 ms 57836 KB Output is correct
96 Correct 266 ms 60664 KB Output is correct
97 Correct 25 ms 36216 KB Output is correct
98 Correct 22 ms 35584 KB Output is correct
99 Correct 162 ms 57836 KB Output is correct
100 Correct 28 ms 36224 KB Output is correct
101 Correct 229 ms 54184 KB Output is correct
102 Correct 264 ms 59256 KB Output is correct
103 Correct 27 ms 36352 KB Output is correct
104 Correct 30 ms 36224 KB Output is correct
105 Correct 27 ms 36088 KB Output is correct
106 Correct 28 ms 36224 KB Output is correct
107 Correct 27 ms 36352 KB Output is correct
108 Correct 1004 ms 147296 KB Output is correct
109 Correct 1735 ms 146252 KB Output is correct
110 Correct 1762 ms 161336 KB Output is correct
111 Correct 1768 ms 165392 KB Output is correct
112 Correct 1680 ms 155272 KB Output is correct
113 Correct 906 ms 146808 KB Output is correct
114 Correct 1283 ms 128676 KB Output is correct
115 Correct 1279 ms 132624 KB Output is correct
116 Correct 1419 ms 130652 KB Output is correct
117 Correct 1278 ms 142704 KB Output is correct
118 Correct 1314 ms 126200 KB Output is correct
119 Correct 1264 ms 142584 KB Output is correct
120 Correct 1624 ms 155908 KB Output is correct
121 Correct 1270 ms 142628 KB Output is correct
122 Correct 1426 ms 136440 KB Output is correct
123 Correct 894 ms 147548 KB Output is correct
124 Correct 1599 ms 139816 KB Output is correct