Submission #973567

# Submission time Handle Problem Language Result Execution time Memory
973567 2024-05-02T07:23:04 Z efedmrlr Unique Cities (JOI19_ho_t5) C++17
4 / 100
2000 ms 47084 KB
// #pragma GCC optimize("O3,Ofast,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>

using namespace std;


#define lli long long int
#define MP make_pair
#define pb push_back
#define REP(i,n) for(int i = 0; (i) < (n); (i)++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()


void fastio() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
}


const double EPS = 0.00001;
const int INF = 1e9+500;
const int N = 3e5+5;
const int ALPH = 26;
const int LGN = 25;
constexpr int MOD = 1e9+7;
int n = 0, m = 0;
int D1 = 0, D2 = 0;
vector<int> t(N, 0);
vector<vector<int> > adj(N, vector<int>());
vector<int> mxsub;
vector<int> res(N, 0), dep, mxs;
vector<int> stck;
vector<int> freq;
int ans;
array<int, 2> get_far(int node, int par, int dist = 0) {
    array<int, 2> ret = {dist, node};
    for(auto c : adj[node]) {
        if(c == par) continue;
        ret = max(ret, get_far(c, node, dist + 1));
    }   
    return ret;
}

void pop_el() {
    freq[t[stck.back()]]--;
    if(freq[t[stck.back()]] == 0) ans--;
    stck.pop_back();
}
void add_el(int x) {
    freq[t[x]]++;
    stck.pb(x);
    if(freq[t[x]] == 1) ans++;
}

int prec(int node, int par) {
    array<array<int, 2>, 2> mx;
    mx[0] = {0, 0}; mx[1] = {0, 0};
    for(auto c : adj[node]) {
        if(c == par) continue;
        dep[c] = dep[node] + 1;
        int ret = prec(c, node);
        if(ret > mx[0][0]) {
            swap(mx[0], mx[1]);
            mx[0] = {ret, c};
        }
        else if(ret > mx[1][0]) {
            mx[1] = {ret, c};
        }
    }
    int ind = -1;
    for(int i = 0; i < adj[node].size(); i++) {
        if(adj[node][i] == par) continue;
        if(mx[0][1] == adj[node][i]) {
            ind = i;
        }
    }
    if(ind != -1) {
        swap(adj[node][ind], adj[node][0]);
    }
    mxs[node] = mx[0][0];
    mxsub[node] = mx[1][0];
    return mx[0][0] + 1;
}

void dfs(int node, int par) {
    // cout << "node:" << node << "\n";
    vector<int> tmp(0);
    
    if(adj[node].size() && adj[node][0] != par) {
        while(stck.size() && dep[stck.back()] >= dep[node] - mxsub[node]) {
            tmp.pb(stck.back());
            pop_el();
        }
        add_el(node);
        dfs(adj[node][0], node);
        pop_el();
    }
    
    while(stck.size() && dep[stck.back()] >= dep[node] - mxs[node]) {
        tmp.pb(stck.back());
        pop_el();
    }
    res[node] = max(res[node], ans);
    add_el(node);
    for(int i = 1; i < adj[node].size(); i++) {
        auto c = adj[node][i];
        if(c == par) continue;
        dfs(c, node);
    }
    pop_el();
    while(tmp.size()) {
        add_el(tmp.back());
        tmp.pop_back();
    }
    
}

void find_res(int rt) {
    mxsub.assign(n + 1, 0);
    dep.assign(n + 1, 0);
    stck.clear();
    mxs.assign(n + 1, 0);
    freq.assign(n + 1, 0);
    ans = 0;
    prec(rt, 0);
    dfs(rt, 0);
}

inline void solve() {
    cin>>n>>m;
    REP(i, n - 1) {
        int u, v;
        cin >> u >> v;
        adj[u].pb(v);
        adj[v].pb(u);
    }
    for(int i = 1; i <= n; i++) {
        cin >> t[i];
    }
    D1 = get_far(1, 0)[1];
    D2 = get_far(D1, 0)[1];
    // cout << D1 << " " << D2 << "\n";
    find_res(D1);
    find_res(D2);
    for(int i = 1; i <= n; i++) {
        cout << res[i] << "\n";
    }
}
 
signed main() {

    fastio();
    int test = 1;
    //cin>>test;
    while(test--) {
        solve();
    }
    
}

Compilation message

joi2019_ho_t5.cpp: In function 'int prec(int, int)':
joi2019_ho_t5.cpp:73:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   73 |     for(int i = 0; i < adj[node].size(); i++) {
      |                    ~~^~~~~~~~~~~~~~~~~~
joi2019_ho_t5.cpp: In function 'void dfs(int, int)':
joi2019_ho_t5.cpp:107:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  107 |     for(int i = 1; i < adj[node].size(); i++) {
      |                    ~~^~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 9816 KB Output is correct
2 Correct 6 ms 10076 KB Output is correct
3 Correct 8 ms 9988 KB Output is correct
4 Correct 9 ms 10076 KB Output is correct
5 Correct 5 ms 9820 KB Output is correct
6 Correct 20 ms 10240 KB Output is correct
7 Correct 8 ms 10076 KB Output is correct
8 Correct 6 ms 9820 KB Output is correct
9 Correct 4 ms 9820 KB Output is correct
10 Correct 5 ms 9820 KB Output is correct
11 Correct 4 ms 9868 KB Output is correct
12 Correct 4 ms 9820 KB Output is correct
13 Correct 18 ms 10124 KB Output is correct
14 Correct 6 ms 10076 KB Output is correct
15 Correct 6 ms 9820 KB Output is correct
16 Correct 4 ms 9820 KB Output is correct
17 Correct 12 ms 10076 KB Output is correct
18 Correct 9 ms 10076 KB Output is correct
19 Correct 5 ms 9820 KB Output is correct
20 Correct 24 ms 10076 KB Output is correct
21 Correct 10 ms 10076 KB Output is correct
22 Correct 5 ms 9816 KB Output is correct
23 Correct 5 ms 9816 KB Output is correct
24 Correct 5 ms 9820 KB Output is correct
25 Correct 4 ms 10016 KB Output is correct
26 Correct 4 ms 9820 KB Output is correct
27 Correct 13 ms 10076 KB Output is correct
28 Correct 12 ms 10076 KB Output is correct
29 Correct 6 ms 10076 KB Output is correct
30 Correct 4 ms 9816 KB Output is correct
31 Correct 13 ms 10076 KB Output is correct
32 Correct 10 ms 10076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 80 ms 16372 KB Output is correct
2 Execution timed out 2047 ms 31808 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 190 ms 19452 KB Output is correct
2 Execution timed out 2062 ms 47084 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 9816 KB Output is correct
2 Correct 6 ms 10076 KB Output is correct
3 Correct 8 ms 9988 KB Output is correct
4 Correct 9 ms 10076 KB Output is correct
5 Correct 5 ms 9820 KB Output is correct
6 Correct 20 ms 10240 KB Output is correct
7 Correct 8 ms 10076 KB Output is correct
8 Correct 6 ms 9820 KB Output is correct
9 Correct 4 ms 9820 KB Output is correct
10 Correct 5 ms 9820 KB Output is correct
11 Correct 4 ms 9868 KB Output is correct
12 Correct 4 ms 9820 KB Output is correct
13 Correct 18 ms 10124 KB Output is correct
14 Correct 6 ms 10076 KB Output is correct
15 Correct 6 ms 9820 KB Output is correct
16 Correct 4 ms 9820 KB Output is correct
17 Correct 12 ms 10076 KB Output is correct
18 Correct 9 ms 10076 KB Output is correct
19 Correct 5 ms 9820 KB Output is correct
20 Correct 24 ms 10076 KB Output is correct
21 Correct 10 ms 10076 KB Output is correct
22 Correct 5 ms 9816 KB Output is correct
23 Correct 5 ms 9816 KB Output is correct
24 Correct 5 ms 9820 KB Output is correct
25 Correct 4 ms 10016 KB Output is correct
26 Correct 4 ms 9820 KB Output is correct
27 Correct 13 ms 10076 KB Output is correct
28 Correct 12 ms 10076 KB Output is correct
29 Correct 6 ms 10076 KB Output is correct
30 Correct 4 ms 9816 KB Output is correct
31 Correct 13 ms 10076 KB Output is correct
32 Correct 10 ms 10076 KB Output is correct
33 Correct 80 ms 16372 KB Output is correct
34 Execution timed out 2047 ms 31808 KB Time limit exceeded
35 Halted 0 ms 0 KB -