Submission #823869

# Submission time Handle Problem Language Result Execution time Memory
823869 2023-08-13T09:04:24 Z 8pete8 Mergers (JOI19_mergers) C++14
0 / 100
3000 ms 33308 KB
#include<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<unordered_map>
#include <queue>
#include<cstring>
#include<limits.h>
#include<cmath>
#include<set>
#include<algorithm>
#include<bitset>
using namespace std;
#define ll long long
#define f first
#define endl "\n"
#define s second
#define pii pair<int,int>
#define ppii pair<pii,int>
#define pb push_back
#define all(x) x.begin(),x.end()
#define F(n) for(int i=0;i<n;i++)
#define lb lower_bound
#define p push
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
using namespace std;
const int mxn=5*1e5,mod=998244353;
void setIO(string name) {
	ios_base::sync_with_stdio(0); cin.tie(0);
	freopen((name+".in").c_str(),"r",stdin);
	freopen((name+".out").c_str(),"w",stdout);
}
int ans=0;
int pa[mxn+10],sz[mxn+10];
vector<int>adj[mxn+10];
int find(int u){
    if(u==pa[u])return u;
    return pa[u]=find(pa[u]);
}
void merg(int u,int v){
    int a=find(u),b=find(v);
    if(a==b)return;
    if(sz[a]>sz[b]){
        pa[b]=a;
        sz[a]+=sz[b];
        return;
    }
    pa[a]=b;
    sz[b]+=sz[a];
}
int c[mxn+10],st[mxn+10];
vector<int>s[mxn+10];
void solve(int cur,int p){
    for(auto i:adj[cur]){
        if(i==p)continue;
        c[i]=cur;
        solve(i,cur);
    }
}
void solve2(int cur,int p){
    int diff=0;
    for(auto i:adj[cur]){
        if(i==p)continue;
        if(st[cur]!=st[i])diff++;
        solve2(i,cur);
    }
    ans+=(diff+1)/2;
}
int32_t main(){
    //setIO("pieaters");
    fastio
    int n,m;cin>>n>>m;
    for(int i=0;i<n-1;i++){
        int u,v;cin>>u>>v;
        adj[u].pb(v);
        adj[v].pb(u);
    }
    for(int i=1;i<=n;i++)cin>>st[i],s[st[i]].pb(i);
    for(int i=1;i<=m;i++)pa[i]=i;
    for(int i=1;i<=m;i++){
        solve(s[i][0],-1);
        for(int j=1;j<s[i].size();j++){
            int cur=c[s[i][j]];
            while(st[cur]!=i)merg(i,st[cur]),cur=c[cur];
        }
    }
    for(int j=1;j<=n;j++)st[j]=find(st[j]);
    solve2(1,-1);
    cout<<ans;
}

Compilation message

mergers.cpp: In function 'int32_t main()':
mergers.cpp:83:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   83 |         for(int j=1;j<s[i].size();j++){
      |                     ~^~~~~~~~~~~~
mergers.cpp: In function 'void setIO(std::string)':
mergers.cpp:31:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   31 |  freopen((name+".in").c_str(),"r",stdin);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mergers.cpp:32:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   32 |  freopen((name+".out").c_str(),"w",stdout);
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 11 ms 23764 KB Output is correct
2 Correct 10 ms 23764 KB Output is correct
3 Incorrect 11 ms 23764 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 11 ms 23764 KB Output is correct
2 Correct 10 ms 23764 KB Output is correct
3 Incorrect 11 ms 23764 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 11 ms 23764 KB Output is correct
2 Correct 10 ms 23764 KB Output is correct
3 Incorrect 11 ms 23764 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 79 ms 30156 KB Output is correct
2 Execution timed out 3076 ms 33308 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 11 ms 23764 KB Output is correct
2 Correct 10 ms 23764 KB Output is correct
3 Incorrect 11 ms 23764 KB Output isn't correct
4 Halted 0 ms 0 KB -