답안 #538545

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
538545 2022-03-17T04:36:27 Z wiwiho Unique Cities (JOI19_ho_t5) C++14
100 / 100
458 ms 50112 KB
#include <bits/stdc++.h>
#include <bits/extc++.h>
 
#define StarBurstStream ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define iter(a) a.begin(), a.end()
#define riter(a) a.rbegin(), a.rend()
#define lsort(a) sort(iter(a))
#define gsort(a) sort(riter(a))
#define pb(a) push_back(a)
#define eb(a) emplace_back(a)
#define pf(a) push_front(a)
#define ef(a) emplace_front(a)
#define pob pop_back()
#define pof pop_front()
#define mp(a, b) make_pair(a, b)
#define F first
#define S second
#define mt make_tuple
#define gt(t, i) get<i>(t)
#define tomax(a, b) ((a) = max((a), (b)))
#define tomin(a, b) ((a) = min((a), (b)))
#define topos(a) ((a) = (((a) % MOD + MOD) % MOD))
#define uni(a) a.resize(unique(iter(a)) - a.begin())
#define printv(a, b) {bool pvaspace=false; \
for(auto pva : a){ \
    if(pvaspace) b << " "; pvaspace=true;\
    b << pva;\
}\
b << "\n";}
 
using namespace std;
using namespace __gnu_pbds;
 
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
 
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;
using tiii = tuple<int, int, int>;
 
const ll MOD = 1000000007;
const ll MAX = 2147483647;
 
template<typename A, typename B>
ostream& operator<<(ostream& o, pair<A, B> p){
    return o << '(' << p.F << ',' << p.S << ')';
}
 
ll ifloor(ll a, ll b){
    if(b < 0) a *= -1, b *= -1;
    if(a < 0) return (a - b + 1) / b;
    else return a / b;
}
 
ll iceil(ll a, ll b){
    if(b < 0) a *= -1, b *= -1;
    if(a > 0) return (a + b - 1) / b;
    else return a / b;
}
 
int n, m;
vector<vector<int>> g;
vector<int> C;
 
void init(){
    g.resize(n + 1);
    C.resize(n + 1);
}
 
int fd = -1, fv = -1;
void dfsfar(int now, int p, int d){
    if(d > fd) fd = d, fv = now;
    for(int i : g[now]){
        if(i == p) continue;
        dfsfar(i, now, d + 1);
    }
}
 
struct Solver{
 
    vector<int> dep, cnt, dis, ans;
    vector<int> st;
    int tans = 0;
 
    void add(int id){
        st.eb(id);
        cnt[C[id]]++;
        if(cnt[C[id]] == 1) tans++;
    }
    void remove(){
        int id = st.back();
        st.pob;
        cnt[C[id]]--;
        if(cnt[C[id]] == 0) tans--;
    }
 
    void dfs1(int now, int p, int d){
        dis[now] = d;
        for(int i : g[now]){
            if(i == p) continue;
            dfs1(i, now, d + 1);
            dep[now] = max(dep[i] + 1, dep[now]);
        }
    }
 
    void dfs2(int now, int p){
        int mx1 = -1, mx2 = -1;
        for(int i : g[now]){
            if(i == p) continue;
            if(mx1 == -1 || dep[i] > dep[mx1]) swap(mx1, i);
            if(i == -1) continue;
            if(mx2 == -1 || dep[i] > dep[mx2]) swap(mx2, i);
        }
        if(mx1 == -1){
            ans[now] = tans;
            //cerr << "dfs2 " << now << " " << mx1 << " " << mx2 << "\n";
            //printv(st, cerr);
            return;
        }
 
        if(mx2 != -1){
            while(!st.empty() && dis[st.back()] >= dis[now] - dep[mx2] - 1) remove();
        }
        add(now);
        dfs2(mx1, now);
        if(!st.empty() && st.back() == now) remove();
 
        while(!st.empty() && dis[st.back()] >= dis[now] - dep[mx1] - 1) remove();
        for(int i : g[now]){
            if(i == p || i == mx1) continue;
            add(now);
            dfs2(i, now);
            if(!st.empty() && st.back() == now) remove();
        }
        //cerr << "dfs2 " << now << " " << mx1 << " " << mx2 << "\n";
        //printv(st, cerr);
        ans[now] = tans;
    }
 
    void solve(int s){
        cnt.resize(n + 1);
        dep.resize(n + 1);
        dis.resize(n + 1);
        ans.resize(n + 1);
 
        dfs1(s, s, 0);
        dfs2(s, s);
 
        /*cerr << "solve " << s << "\n";
        printv(dep, cerr);
        printv(dis, cerr);
        printv(ans, cerr);*/
    }
 
};

namespace BF{
    vector<int> dpt;
    void dfsbf(int now, int p){
        dpt[now] = now == p ? 0 : dpt[p] + 1;
        for(int i : g[now]){
            if(i == p) continue;
            dfsbf(i, now);
        }
    }

    int bf(int s){
        dpt.clear();
        dpt.resize(n + 1);
        dfsbf(s, s);
        vector<vector<int>> t(n + 1);
        vector<int> tv;
        for(int i = 1; i <= n; i++){
            t[dpt[i]].eb(i);
        }
        for(int i = 1; i <= n; i++){
            if(t[i].size() != 1) continue;
            tv.eb(C[t[i][0]]);
        }
        lsort(tv);
        uni(tv);
        return tv.size();
    }
}
 
int main(){
    StarBurstStream
 
    //int seed = 10650;
    //int seed = clock();
    //mt19937 rnd(seed);
    //cerr << seed << "\n";
    //n = 10; m = 10;
    cin >> n >> m;
    //cerr << n << " " << m << "\n";
    init();
    for(int i = 0; i < n - 1; i++){
        int u, v;
        cin >> u >> v;
        /*u = i + 2;
        uniform_int_distribution<int> ud(1, i + 1);
        v = ud(rnd);*/
        g[u].eb(v);
        g[v].eb(u);
        //cerr << u << " " << v << "\n";
    }
    for(int i = 1; i <= n; i++){
        cin >> C[i];
        //uniform_int_distribution<int> ud(1, m);
        //C[i] = ud(rnd);
        //cerr << C[i] << " ";
    }
    //cerr << "\n";
 
    dfsfar(1, 1, 0);
    int v1 = fv;
    fv = -1; fd = -1;
    dfsfar(v1, v1, 0);
    int v2 = fv;
    //cerr << v1 << " " << v2 << "\n";
 
    Solver s1, s2;
    s1.solve(v1);
    s2.solve(v2);
 
    for(int i = 1; i <= n; i++){
        //ll tans = BF::bf(i);
        ll ans = max(s1.ans[i], s2.ans[i]);
        //cerr << "test " << i << " : " << ans << " " << tans << "\n";
        //assert(tans == ans);
        cout << ans << "\n";
    }
 
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 2 ms 468 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 2 ms 596 KB Output is correct
5 Correct 2 ms 468 KB Output is correct
6 Correct 2 ms 724 KB Output is correct
7 Correct 2 ms 596 KB Output is correct
8 Correct 2 ms 468 KB Output is correct
9 Correct 2 ms 468 KB Output is correct
10 Correct 2 ms 468 KB Output is correct
11 Correct 2 ms 468 KB Output is correct
12 Correct 2 ms 468 KB Output is correct
13 Correct 2 ms 724 KB Output is correct
14 Correct 2 ms 596 KB Output is correct
15 Correct 3 ms 468 KB Output is correct
16 Correct 2 ms 468 KB Output is correct
17 Correct 2 ms 596 KB Output is correct
18 Correct 2 ms 596 KB Output is correct
19 Correct 2 ms 468 KB Output is correct
20 Correct 2 ms 724 KB Output is correct
21 Correct 1 ms 596 KB Output is correct
22 Correct 2 ms 468 KB Output is correct
23 Correct 2 ms 468 KB Output is correct
24 Correct 2 ms 468 KB Output is correct
25 Correct 2 ms 468 KB Output is correct
26 Correct 1 ms 468 KB Output is correct
27 Correct 2 ms 596 KB Output is correct
28 Correct 2 ms 596 KB Output is correct
29 Correct 2 ms 596 KB Output is correct
30 Correct 1 ms 468 KB Output is correct
31 Correct 2 ms 596 KB Output is correct
32 Correct 2 ms 596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 160 ms 11000 KB Output is correct
2 Correct 174 ms 27892 KB Output is correct
3 Correct 31 ms 5048 KB Output is correct
4 Correct 360 ms 21700 KB Output is correct
5 Correct 421 ms 48552 KB Output is correct
6 Correct 389 ms 34696 KB Output is correct
7 Correct 348 ms 21780 KB Output is correct
8 Correct 295 ms 24304 KB Output is correct
9 Correct 370 ms 23560 KB Output is correct
10 Correct 298 ms 23504 KB Output is correct
11 Correct 176 ms 22476 KB Output is correct
12 Correct 396 ms 38080 KB Output is correct
13 Correct 395 ms 34504 KB Output is correct
14 Correct 329 ms 33664 KB Output is correct
15 Correct 145 ms 22348 KB Output is correct
16 Correct 300 ms 40312 KB Output is correct
17 Correct 377 ms 34884 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 223 ms 14900 KB Output is correct
2 Correct 445 ms 47852 KB Output is correct
3 Correct 31 ms 5836 KB Output is correct
4 Correct 355 ms 22596 KB Output is correct
5 Correct 457 ms 50112 KB Output is correct
6 Correct 424 ms 35916 KB Output is correct
7 Correct 303 ms 22780 KB Output is correct
8 Correct 399 ms 27940 KB Output is correct
9 Correct 318 ms 26088 KB Output is correct
10 Correct 338 ms 24624 KB Output is correct
11 Correct 223 ms 22856 KB Output is correct
12 Correct 458 ms 44404 KB Output is correct
13 Correct 357 ms 34020 KB Output is correct
14 Correct 353 ms 34256 KB Output is correct
15 Correct 142 ms 23340 KB Output is correct
16 Correct 328 ms 41268 KB Output is correct
17 Correct 360 ms 36040 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 2 ms 468 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 2 ms 596 KB Output is correct
5 Correct 2 ms 468 KB Output is correct
6 Correct 2 ms 724 KB Output is correct
7 Correct 2 ms 596 KB Output is correct
8 Correct 2 ms 468 KB Output is correct
9 Correct 2 ms 468 KB Output is correct
10 Correct 2 ms 468 KB Output is correct
11 Correct 2 ms 468 KB Output is correct
12 Correct 2 ms 468 KB Output is correct
13 Correct 2 ms 724 KB Output is correct
14 Correct 2 ms 596 KB Output is correct
15 Correct 3 ms 468 KB Output is correct
16 Correct 2 ms 468 KB Output is correct
17 Correct 2 ms 596 KB Output is correct
18 Correct 2 ms 596 KB Output is correct
19 Correct 2 ms 468 KB Output is correct
20 Correct 2 ms 724 KB Output is correct
21 Correct 1 ms 596 KB Output is correct
22 Correct 2 ms 468 KB Output is correct
23 Correct 2 ms 468 KB Output is correct
24 Correct 2 ms 468 KB Output is correct
25 Correct 2 ms 468 KB Output is correct
26 Correct 1 ms 468 KB Output is correct
27 Correct 2 ms 596 KB Output is correct
28 Correct 2 ms 596 KB Output is correct
29 Correct 2 ms 596 KB Output is correct
30 Correct 1 ms 468 KB Output is correct
31 Correct 2 ms 596 KB Output is correct
32 Correct 2 ms 596 KB Output is correct
33 Correct 160 ms 11000 KB Output is correct
34 Correct 174 ms 27892 KB Output is correct
35 Correct 31 ms 5048 KB Output is correct
36 Correct 360 ms 21700 KB Output is correct
37 Correct 421 ms 48552 KB Output is correct
38 Correct 389 ms 34696 KB Output is correct
39 Correct 348 ms 21780 KB Output is correct
40 Correct 295 ms 24304 KB Output is correct
41 Correct 370 ms 23560 KB Output is correct
42 Correct 298 ms 23504 KB Output is correct
43 Correct 176 ms 22476 KB Output is correct
44 Correct 396 ms 38080 KB Output is correct
45 Correct 395 ms 34504 KB Output is correct
46 Correct 329 ms 33664 KB Output is correct
47 Correct 145 ms 22348 KB Output is correct
48 Correct 300 ms 40312 KB Output is correct
49 Correct 377 ms 34884 KB Output is correct
50 Correct 223 ms 14900 KB Output is correct
51 Correct 445 ms 47852 KB Output is correct
52 Correct 31 ms 5836 KB Output is correct
53 Correct 355 ms 22596 KB Output is correct
54 Correct 457 ms 50112 KB Output is correct
55 Correct 424 ms 35916 KB Output is correct
56 Correct 303 ms 22780 KB Output is correct
57 Correct 399 ms 27940 KB Output is correct
58 Correct 318 ms 26088 KB Output is correct
59 Correct 338 ms 24624 KB Output is correct
60 Correct 223 ms 22856 KB Output is correct
61 Correct 458 ms 44404 KB Output is correct
62 Correct 357 ms 34020 KB Output is correct
63 Correct 353 ms 34256 KB Output is correct
64 Correct 142 ms 23340 KB Output is correct
65 Correct 328 ms 41268 KB Output is correct
66 Correct 360 ms 36040 KB Output is correct
67 Correct 20 ms 3164 KB Output is correct
68 Correct 112 ms 21724 KB Output is correct
69 Correct 205 ms 22156 KB Output is correct
70 Correct 331 ms 21788 KB Output is correct
71 Correct 432 ms 48596 KB Output is correct
72 Correct 377 ms 34668 KB Output is correct
73 Correct 320 ms 21720 KB Output is correct
74 Correct 309 ms 26632 KB Output is correct
75 Correct 348 ms 24056 KB Output is correct
76 Correct 292 ms 23628 KB Output is correct
77 Correct 215 ms 21984 KB Output is correct
78 Correct 394 ms 41004 KB Output is correct
79 Correct 404 ms 37752 KB Output is correct
80 Correct 352 ms 32316 KB Output is correct
81 Correct 146 ms 22236 KB Output is correct
82 Correct 323 ms 40240 KB Output is correct
83 Correct 365 ms 34932 KB Output is correct
84 Correct 324 ms 21996 KB Output is correct
85 Correct 448 ms 48992 KB Output is correct
86 Correct 408 ms 35160 KB Output is correct
87 Correct 315 ms 22060 KB Output is correct
88 Correct 350 ms 27380 KB Output is correct
89 Correct 339 ms 24856 KB Output is correct
90 Correct 340 ms 24168 KB Output is correct
91 Correct 225 ms 22436 KB Output is correct
92 Correct 428 ms 48036 KB Output is correct
93 Correct 348 ms 31084 KB Output is correct
94 Correct 346 ms 28672 KB Output is correct
95 Correct 117 ms 22560 KB Output is correct
96 Correct 359 ms 40564 KB Output is correct
97 Correct 323 ms 35528 KB Output is correct
98 Correct 369 ms 22596 KB Output is correct
99 Correct 423 ms 49520 KB Output is correct
100 Correct 407 ms 35708 KB Output is correct
101 Correct 312 ms 22464 KB Output is correct
102 Correct 335 ms 26704 KB Output is correct
103 Correct 334 ms 25144 KB Output is correct
104 Correct 355 ms 24464 KB Output is correct
105 Correct 222 ms 22804 KB Output is correct
106 Correct 396 ms 37488 KB Output is correct
107 Correct 363 ms 38328 KB Output is correct
108 Correct 324 ms 30644 KB Output is correct
109 Correct 179 ms 23268 KB Output is correct
110 Correct 315 ms 40948 KB Output is correct
111 Correct 367 ms 35844 KB Output is correct