답안 #959180

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
959180 2024-04-07T14:50:15 Z andrei_boaca 수도 (JOI20_capital_city) C++17
11 / 100
3000 ms 382160 KB
#include <bits/stdc++.h>

using namespace std;
typedef pair<int,int> pii;
int n,k,nr[200005];
bool use[200005];
vector<int> mynodes[200005];
int cul[200005];
vector<int> muchii[200005];
vector<int> edges[200005];
vector<pii> g;
vector<int> st;
vector<pii> init;
int comp[200005],nrcul[200005],nrcomp,nrnodes[200005],nrmuchii[200005];
int root;
void dfs1(int nod)
{
    use[nod]=1;
    for(int i:edges[nod])
        if(!use[i])
            dfs1(i);
    st.push_back(nod);
}
void dfs2(int nod)
{
    use[nod]=1;
    comp[nod]=nrcomp;
    nrcul[nrcomp]++;
    for(int i:edges[nod])
        if(!use[i])
            dfs2(i);
}
void dfs(int nod)
{
    use[nod]=1;
    nr[nod]=0;
    nr[nod]=(cul[nod]==cul[root]);
    for(int i:muchii[nod])
        if(!use[i])
        {
            dfs(i);
            nr[nod]+=nr[i];
        }
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin>>n>>k;
    for(int i=1;i<n;i++)
    {
        int a,b;
        cin>>a>>b;
        muchii[a].push_back(b);
        muchii[b].push_back(a);
        init.push_back({a,b});
    }
    for(int i=1;i<=n;i++)
    {
        cin>>cul[i];
        mynodes[cul[i]].push_back(i);
    }
    for(int c=1;c<=k;c++)
    {
        root=mynodes[c][0];
        for(int i=1;i<=n;i++)
            use[i]=0;
        dfs(root);
        for(int i=1;i<=n;i++)
            if(nr[i]>0&&cul[i]!=c)
            {
                edges[c].push_back(cul[i]);
                g.push_back({c,cul[i]});
            }
    }
    for(int i=1;i<=k;i++)
        use[i]=0;
    for(int i=1;i<=k;i++)
        if(!use[i])
            dfs1(i);
    reverse(st.begin(),st.end());
    for(int i=1;i<=k;i++)
    {
        use[i]=0;
        edges[i].clear();
    }
    for(pii p:g)
        edges[p.second].push_back(p.first);
    for(int i:st)
        if(!use[i])
        {
            nrcomp++;
            dfs2(i);
        }
    for(int i=1;i<=n;i++)
    {
        int c=comp[cul[i]];
        nrnodes[c]++;
    }
    for(pii p:init)
    {
        int a=p.first;
        int b=p.second;
        a=comp[cul[a]];
        b=comp[cul[b]];
        if(a==b)
            nrmuchii[a]++;
    }
    int ans=1e9;
    for(int i=1;i<=nrcomp;i++)
        if(nrmuchii[i]+1==nrnodes[i])
            ans=min(ans,nrcul[i]);
    cout<<ans-1;
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 16732 KB Output is correct
2 Correct 4 ms 16848 KB Output is correct
3 Correct 4 ms 16732 KB Output is correct
4 Correct 4 ms 16732 KB Output is correct
5 Correct 3 ms 16732 KB Output is correct
6 Correct 4 ms 16732 KB Output is correct
7 Correct 4 ms 16732 KB Output is correct
8 Correct 3 ms 16732 KB Output is correct
9 Correct 5 ms 16880 KB Output is correct
10 Correct 4 ms 16892 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 16732 KB Output is correct
2 Correct 4 ms 16848 KB Output is correct
3 Correct 4 ms 16732 KB Output is correct
4 Correct 4 ms 16732 KB Output is correct
5 Correct 3 ms 16732 KB Output is correct
6 Correct 4 ms 16732 KB Output is correct
7 Correct 4 ms 16732 KB Output is correct
8 Correct 3 ms 16732 KB Output is correct
9 Correct 5 ms 16880 KB Output is correct
10 Correct 4 ms 16892 KB Output is correct
11 Correct 11 ms 17128 KB Output is correct
12 Correct 9 ms 17240 KB Output is correct
13 Correct 9 ms 16988 KB Output is correct
14 Correct 9 ms 16988 KB Output is correct
15 Correct 19 ms 17724 KB Output is correct
16 Correct 21 ms 18736 KB Output is correct
17 Correct 19 ms 22020 KB Output is correct
18 Correct 18 ms 21924 KB Output is correct
19 Correct 19 ms 23828 KB Output is correct
20 Correct 19 ms 22024 KB Output is correct
21 Correct 22 ms 22032 KB Output is correct
22 Correct 42 ms 32768 KB Output is correct
23 Correct 49 ms 32592 KB Output is correct
24 Correct 23 ms 17088 KB Output is correct
25 Correct 19 ms 17496 KB Output is correct
26 Correct 20 ms 17244 KB Output is correct
27 Correct 18 ms 17500 KB Output is correct
28 Correct 16 ms 17728 KB Output is correct
29 Correct 17 ms 17984 KB Output is correct
30 Correct 16 ms 17724 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3033 ms 382160 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 16732 KB Output is correct
2 Correct 4 ms 16848 KB Output is correct
3 Correct 4 ms 16732 KB Output is correct
4 Correct 4 ms 16732 KB Output is correct
5 Correct 3 ms 16732 KB Output is correct
6 Correct 4 ms 16732 KB Output is correct
7 Correct 4 ms 16732 KB Output is correct
8 Correct 3 ms 16732 KB Output is correct
9 Correct 5 ms 16880 KB Output is correct
10 Correct 4 ms 16892 KB Output is correct
11 Correct 11 ms 17128 KB Output is correct
12 Correct 9 ms 17240 KB Output is correct
13 Correct 9 ms 16988 KB Output is correct
14 Correct 9 ms 16988 KB Output is correct
15 Correct 19 ms 17724 KB Output is correct
16 Correct 21 ms 18736 KB Output is correct
17 Correct 19 ms 22020 KB Output is correct
18 Correct 18 ms 21924 KB Output is correct
19 Correct 19 ms 23828 KB Output is correct
20 Correct 19 ms 22024 KB Output is correct
21 Correct 22 ms 22032 KB Output is correct
22 Correct 42 ms 32768 KB Output is correct
23 Correct 49 ms 32592 KB Output is correct
24 Correct 23 ms 17088 KB Output is correct
25 Correct 19 ms 17496 KB Output is correct
26 Correct 20 ms 17244 KB Output is correct
27 Correct 18 ms 17500 KB Output is correct
28 Correct 16 ms 17728 KB Output is correct
29 Correct 17 ms 17984 KB Output is correct
30 Correct 16 ms 17724 KB Output is correct
31 Execution timed out 3033 ms 382160 KB Time limit exceeded
32 Halted 0 ms 0 KB -