Submission #799795

# Submission time Handle Problem Language Result Execution time Memory
799795 2023-08-01T03:24:02 Z 1075508020060209tc Unique Cities (JOI19_ho_t5) C++14
4 / 100
2000 ms 53508 KB
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC targent("avx,popcnt,sse4,abm")
#include <bits/stdc++.h>
using namespace std;
#define int long long

int n;int M;
vector<int>e[500005];
int D;
int rt1;int rt2;
int dph[500005];
int dp[500005];
int fa[500005];

void dpdfs(int nw,int pa){
fa[nw]=pa;
dph[nw]=dph[pa]+1;
dp[nw]=nw;
for(int i=0;i<e[nw].size();i++){
    int v=e[nw][i];
    if(v==pa){continue;}
    dpdfs(v,nw);
    if(dph[dp[v]]>dph[dp[nw]]){
        dp[nw]=dp[v];
    }
}
}

void finrt(){
dpdfs(1,0);
rt1=dp[1];
//cout<<rt1<<" ";
dpdfs(rt1,0);
rt2=dp[rt1];
//cout<<rt2<<endl;
D=dph[rt2];
}

void srtlng(int rt){
dpdfs(rt,0);
for(int nw=1;nw<=n;nw++){
    for(int i=0;i<e[nw].size();i++){
        int v=e[nw][i];
        if(v==fa[nw]){continue;}
        if(e[nw][0]==fa[nw]||dph[dp[v]]>dph[dp[e[nw][0]]]){
            swap(e[nw][i],e[nw][0]);
        }
    }
    for(int i=1;i<e[nw].size();i++){
        int v=e[nw][i];
        if(v==fa[nw]){continue;}
        if(e[nw][1]==fa[nw]||dph[dp[v]]>dph[dp[e[nw][1]]]){
            swap(e[nw][i],e[nw][1]);
        }
    }
}
}
int ar[500005];
int buc[500005];
int bcnt;
void add(int v){
    buc[v]++;
    if(buc[v]==1){bcnt++;}
    //cout<<v<<" "<<bcnt<<endl;
}
void del(int v){
    buc[v]--;
    if(buc[v]==0){bcnt--;}
   // cout<<v<<" "<<bcnt<<endl;
}
stack<int>stk;


int ans[500005];
void dfs(int nw,int pa){
if(e[nw].size()==1&&pa){
    ans[nw]=max(ans[nw],bcnt);
    return;
}
vector<int>rev;
if(1){
    int d=0;
    if(e[nw].size()>=2&&e[nw][1]!=pa){
        d=dph[dp[e[nw][1]]]-dph[nw];
    }
    while(stk.size()&&dph[stk.top()]>=dph[nw]-d){
        rev.push_back(stk.top());
        del(ar[stk.top()]);
        stk.pop();
    }
    stk.push(nw);
    add(ar[nw]);
    dfs(e[nw][0],nw);
    del(ar[nw]);
    stk.pop();
}
int d=dph[dp[e[nw][0]]]-dph[nw];
while(stk.size()&&dph[stk.top()]>=dph[nw]-d){
    rev.push_back(stk.top());
    del(ar[stk.top()]);
    stk.pop();
}
ans[nw]=max(ans[nw],bcnt);
add(ar[nw]);
stk.push(nw);
for(int i=1;i<e[nw].size();i++){
    int v=e[nw][i];
    if(v==pa){continue;}
    dfs(v,nw);
}
del(ar[nw]);
stk.pop();
while(rev.size()){
    add(ar[rev.back()]);
    stk.push(rev.back());
    rev.pop_back();
}
}

void solve(int rt){
bcnt=0;
while(stk.size()){stk.pop();}
for(int i=1;i<=n;i++){
    buc[i]=0;
}
dpdfs(rt,0);
srtlng(rt);
dfs(rt,0);
}


signed main(){
cin.tie(0);
ios_base::sync_with_stdio(0);
cin>>n>>M;
for(int i=1;i<=n-1;i++){
    int a;int b;
    cin>>a>>b;
    e[a].push_back(b);
    e[b].push_back(a);
}
for(int i=1;i<=n;i++){cin>>ar[i];}
finrt();
//cout<<rt1<<endl;
solve(rt1);
//cout<<endl;

//cout<<rt2<<endl;
solve(rt2);
//cout<<rt1<<" "<<rt2<<endl;
for(int i=1;i<=n;i++){
    cout<<ans[i]<<endl;
}
}

Compilation message

joi2019_ho_t5.cpp:2: warning: ignoring '#pragma GCC targent' [-Wunknown-pragmas]
    2 | #pragma GCC targent("avx,popcnt,sse4,abm")
      | 
joi2019_ho_t5.cpp: In function 'void dpdfs(long long int, long long int)':
joi2019_ho_t5.cpp:19:14: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   19 | for(int i=0;i<e[nw].size();i++){
      |             ~^~~~~~~~~~~~~
joi2019_ho_t5.cpp: In function 'void srtlng(long long int)':
joi2019_ho_t5.cpp:42:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   42 |     for(int i=0;i<e[nw].size();i++){
      |                 ~^~~~~~~~~~~~~
joi2019_ho_t5.cpp:49:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   49 |     for(int i=1;i<e[nw].size();i++){
      |                 ~^~~~~~~~~~~~~
joi2019_ho_t5.cpp: In function 'void dfs(long long int, long long int)':
joi2019_ho_t5.cpp:106:14: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  106 | for(int i=1;i<e[nw].size();i++){
      |             ~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 7 ms 11988 KB Output is correct
2 Correct 9 ms 12244 KB Output is correct
3 Correct 12 ms 12212 KB Output is correct
4 Correct 13 ms 12348 KB Output is correct
5 Correct 9 ms 12244 KB Output is correct
6 Correct 27 ms 12520 KB Output is correct
7 Correct 14 ms 12372 KB Output is correct
8 Correct 10 ms 12244 KB Output is correct
9 Correct 11 ms 12220 KB Output is correct
10 Correct 9 ms 12244 KB Output is correct
11 Correct 9 ms 12244 KB Output is correct
12 Correct 9 ms 12220 KB Output is correct
13 Correct 24 ms 12500 KB Output is correct
14 Correct 11 ms 12356 KB Output is correct
15 Correct 11 ms 12372 KB Output is correct
16 Correct 9 ms 12216 KB Output is correct
17 Correct 17 ms 12448 KB Output is correct
18 Correct 14 ms 12412 KB Output is correct
19 Correct 10 ms 12244 KB Output is correct
20 Correct 28 ms 12524 KB Output is correct
21 Correct 14 ms 12348 KB Output is correct
22 Correct 10 ms 12200 KB Output is correct
23 Correct 10 ms 12244 KB Output is correct
24 Correct 10 ms 12244 KB Output is correct
25 Correct 9 ms 12220 KB Output is correct
26 Correct 9 ms 12244 KB Output is correct
27 Correct 20 ms 12508 KB Output is correct
28 Correct 15 ms 12372 KB Output is correct
29 Correct 12 ms 12372 KB Output is correct
30 Correct 9 ms 12244 KB Output is correct
31 Correct 17 ms 12372 KB Output is correct
32 Correct 14 ms 12332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 210 ms 21972 KB Output is correct
2 Execution timed out 2085 ms 34908 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 342 ms 25644 KB Output is correct
2 Execution timed out 2082 ms 53508 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 11988 KB Output is correct
2 Correct 9 ms 12244 KB Output is correct
3 Correct 12 ms 12212 KB Output is correct
4 Correct 13 ms 12348 KB Output is correct
5 Correct 9 ms 12244 KB Output is correct
6 Correct 27 ms 12520 KB Output is correct
7 Correct 14 ms 12372 KB Output is correct
8 Correct 10 ms 12244 KB Output is correct
9 Correct 11 ms 12220 KB Output is correct
10 Correct 9 ms 12244 KB Output is correct
11 Correct 9 ms 12244 KB Output is correct
12 Correct 9 ms 12220 KB Output is correct
13 Correct 24 ms 12500 KB Output is correct
14 Correct 11 ms 12356 KB Output is correct
15 Correct 11 ms 12372 KB Output is correct
16 Correct 9 ms 12216 KB Output is correct
17 Correct 17 ms 12448 KB Output is correct
18 Correct 14 ms 12412 KB Output is correct
19 Correct 10 ms 12244 KB Output is correct
20 Correct 28 ms 12524 KB Output is correct
21 Correct 14 ms 12348 KB Output is correct
22 Correct 10 ms 12200 KB Output is correct
23 Correct 10 ms 12244 KB Output is correct
24 Correct 10 ms 12244 KB Output is correct
25 Correct 9 ms 12220 KB Output is correct
26 Correct 9 ms 12244 KB Output is correct
27 Correct 20 ms 12508 KB Output is correct
28 Correct 15 ms 12372 KB Output is correct
29 Correct 12 ms 12372 KB Output is correct
30 Correct 9 ms 12244 KB Output is correct
31 Correct 17 ms 12372 KB Output is correct
32 Correct 14 ms 12332 KB Output is correct
33 Correct 210 ms 21972 KB Output is correct
34 Execution timed out 2085 ms 34908 KB Time limit exceeded
35 Halted 0 ms 0 KB -