Submission #1040453

# Submission time Handle Problem Language Result Execution time Memory
1040453 2024-08-01T04:30:07 Z vjudge1 Simurgh (IOI17_simurgh) C++17
100 / 100
70 ms 6708 KB
#include "simurgh.h"
#include<bits/stdc++.h>
#define Z 510
using namespace std;
vector<int>R;
int N_,K_;
int indexofr[Z][Z],deg[Z],royaltree[Z],parind[Z],acpar[Z],dep[Z];
vector<pair<int,int>>tre;
vector<int>U,V;
vector<int>AADJ[Z],bak[Z];
bitset<Z>vis,col,don;
void assert2(int k){
    if(!k){
        vector<int>v;
        for(int i=0;;i++)
            v.push_back(i);
    }
}
void AD(int x,int y){
    R.push_back(indexofr[x][y]-1);
}
int Q(){
    return count_common_roads(R);
}
int Q(vector<int>v){
    for(auto&i:v)i--;
    return count_common_roads(v);
}
struct dsu{
    int par[501];
    int abp(int n){
        return par[n]?par[n]=abp(par[n]):n;
    }
    bool merge(int a,int b){
        a=abp(a+1),b=abp(b+1);
        if(a==b) return 0;
        return par[a]=b;
    }
    void reset(){
        memset(par,0,sizeof par);
    }
} tis, TIS2;
vector<int>path{-1};
void calcfortree(int n){
    path.push_back(n);
    for(auto i:AADJ[n])
        if(acpar[i]==n)
            calcfortree(i);
    int d=1e9;
    for(auto i:AADJ[n])
        d=min(d,dep[i]);
    if(d<dep[n]-1){
        vector<int>V;
        int D=indexofr[n][path[d]];
        for(auto[x,y]:tre)
            V.push_back(indexofr[x][y]);
        V.push_back(D);
        vector<int>undet,det,path2=path;
        path.pop_back();
        while(path2.size()>d+1) {
            if(don[parind[path2.back()]])
                det.push_back(path2.back());
            else undet.push_back(path2.back()),
                col[parind[path2.back()]]=0;
            don[parind[path2.back()]]=1;path2.pop_back();
        }
        if(undet.empty()) return;
        if(det.empty()){
            map<int,vector<int>>mp;
            for(auto i:undet) {
                int RD=indexofr[i][acpar[i]];
                V.erase(find(V.begin(),V.end(),RD));
                mp[Q(V)].push_back(i);
                V.push_back(RD);
            }
            V.erase(find(V.begin(),V.end(),D));
            mp[Q(V)].push_back(N_);
            V.push_back(D);
            if(mp.size()==2)
                for(auto i:mp.begin()->second)
                    if(i<N_) col[parind[i]]=1;
        } else {
            int rk=det[0];
            int RD=indexofr[rk][acpar[rk]];
            V.erase(find(V.begin(),V.end(),RD));
            int akt=Q(V)+col[parind[rk]];
            V.push_back(RD);
            for(auto i:undet){
                RD=indexofr[i][acpar[i]];
                V.erase(find(V.begin(),V.end(),RD));
                col[parind[i]]=akt!=Q(V);
                V.push_back(RD);
            }
        }
    } else path.pop_back();
}
void gendfstree(int n,int p){
    vis[n]=1,dep[n]=dep[p]+1;
    for(auto i:AADJ[n]){
        if(i==p)continue;
        if(!vis[i]) acpar[i]=n,
            parind[i]=tre.size(),
            tre.push_back({n,i}),
            gendfstree(i,n);
    }
    if(!n)calcfortree(n);
}
vector<int>ans;
int countst(vector<int> x,int pivot){
    R.clear();
    tis.reset();
    for(auto i:x)AD(pivot,i),
        tis.merge(pivot,i);
    int K=0;
    for(auto[X,Y]:tre)
        if(tis.merge(X,Y))
            AD(X,Y),K-=col[parind[Y]];
    return K+Q();
}
std::vector<int> find_roads(int n, std::vector<int> u, std::vector<int> v) {
    for(int i=0;i<n;i++)
        col[i]=1;
    U=u;
    V=v;
    N_=n;
    int m=u.size();
    for(int i=0;i<m;i++)
        AADJ[u[i]].push_back(v[i]),
        AADJ[v[i]].push_back(u[i]),
        indexofr[u[i]][v[i]]=indexofr[v[i]][u[i]]=i+1;
    gendfstree(0,0);
    for(auto[X,Y]:tre)
        AD(X,Y);
    for(int i=0;i<n;i++){
        vector<int>v;
        for(int j=0;j<n;j++)
            if(indexofr[i][j])
                v.push_back(j);
        deg[i]=countst(v,i);
    }
    set<int>st;
    for(int i=0;i<n;i++)
        st.insert(i);
    vector<int>done;
    for(int i=1;i<n;i++){
        int x=0;
        for(int i=0;i<n;i++)
            if(deg[i]==1)x=i;
        R.clear();
        deg[x]=0;
        st.erase(x);
        vector<int>V;
        for(auto i:st)
            if(indexofr[i][x])
                V.push_back(i);
        while(V.size()>1){
            vector<int>a[2];
            for(int i=0;i<V.size();i++)
                a[i%2].push_back(V[i]);
            V=a[countst(a[1],x)];
        }
        deg[V[0]]--;
        ans.push_back(indexofr[x][V[0]]-1);
    }
    R=ans;
    //assert(Q()==n-1);
    return ans;
}

Compilation message

simurgh.cpp: In member function 'bool dsu::merge(int, int)':
simurgh.cpp:37:22: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
   37 |         return par[a]=b;
      |                ~~~~~~^~
simurgh.cpp: In function 'void calcfortree(int)':
simurgh.cpp:60:27: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   60 |         while(path2.size()>d+1) {
      |               ~~~~~~~~~~~~^~~~
simurgh.cpp: In function 'std::vector<int> find_roads(int, std::vector<int>, std::vector<int>)':
simurgh.cpp:158:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  158 |             for(int i=0;i<V.size();i++)
      |                         ~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB correct
2 Correct 0 ms 348 KB correct
3 Correct 0 ms 348 KB correct
4 Correct 0 ms 348 KB correct
5 Correct 0 ms 348 KB correct
6 Correct 0 ms 348 KB correct
7 Correct 0 ms 348 KB correct
8 Correct 0 ms 348 KB correct
9 Correct 0 ms 348 KB correct
10 Correct 0 ms 348 KB correct
11 Correct 0 ms 348 KB correct
12 Correct 0 ms 348 KB correct
13 Correct 0 ms 412 KB correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB correct
2 Correct 0 ms 348 KB correct
3 Correct 0 ms 348 KB correct
4 Correct 0 ms 348 KB correct
5 Correct 0 ms 348 KB correct
6 Correct 0 ms 348 KB correct
7 Correct 0 ms 348 KB correct
8 Correct 0 ms 348 KB correct
9 Correct 0 ms 348 KB correct
10 Correct 0 ms 348 KB correct
11 Correct 0 ms 348 KB correct
12 Correct 0 ms 348 KB correct
13 Correct 0 ms 412 KB correct
14 Correct 1 ms 600 KB correct
15 Correct 1 ms 604 KB correct
16 Correct 1 ms 604 KB correct
17 Correct 1 ms 604 KB correct
18 Correct 1 ms 604 KB correct
19 Correct 1 ms 604 KB correct
20 Correct 1 ms 604 KB correct
21 Correct 1 ms 604 KB correct
22 Correct 1 ms 604 KB correct
23 Correct 1 ms 604 KB correct
24 Correct 1 ms 604 KB correct
25 Correct 0 ms 348 KB correct
26 Correct 1 ms 604 KB correct
27 Correct 1 ms 604 KB correct
28 Correct 0 ms 604 KB correct
29 Correct 0 ms 604 KB correct
30 Correct 1 ms 604 KB correct
31 Correct 1 ms 604 KB correct
32 Correct 1 ms 604 KB correct
33 Correct 1 ms 604 KB correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB correct
2 Correct 0 ms 348 KB correct
3 Correct 0 ms 348 KB correct
4 Correct 0 ms 348 KB correct
5 Correct 0 ms 348 KB correct
6 Correct 0 ms 348 KB correct
7 Correct 0 ms 348 KB correct
8 Correct 0 ms 348 KB correct
9 Correct 0 ms 348 KB correct
10 Correct 0 ms 348 KB correct
11 Correct 0 ms 348 KB correct
12 Correct 0 ms 348 KB correct
13 Correct 0 ms 412 KB correct
14 Correct 1 ms 600 KB correct
15 Correct 1 ms 604 KB correct
16 Correct 1 ms 604 KB correct
17 Correct 1 ms 604 KB correct
18 Correct 1 ms 604 KB correct
19 Correct 1 ms 604 KB correct
20 Correct 1 ms 604 KB correct
21 Correct 1 ms 604 KB correct
22 Correct 1 ms 604 KB correct
23 Correct 1 ms 604 KB correct
24 Correct 1 ms 604 KB correct
25 Correct 0 ms 348 KB correct
26 Correct 1 ms 604 KB correct
27 Correct 1 ms 604 KB correct
28 Correct 0 ms 604 KB correct
29 Correct 0 ms 604 KB correct
30 Correct 1 ms 604 KB correct
31 Correct 1 ms 604 KB correct
32 Correct 1 ms 604 KB correct
33 Correct 1 ms 604 KB correct
34 Correct 13 ms 2140 KB correct
35 Correct 13 ms 2136 KB correct
36 Correct 11 ms 1884 KB correct
37 Correct 5 ms 1116 KB correct
38 Correct 12 ms 2232 KB correct
39 Correct 12 ms 1884 KB correct
40 Correct 12 ms 1780 KB correct
41 Correct 12 ms 2140 KB correct
42 Correct 12 ms 2176 KB correct
43 Correct 9 ms 1624 KB correct
44 Correct 9 ms 1372 KB correct
45 Correct 10 ms 1636 KB correct
46 Correct 9 ms 1400 KB correct
47 Correct 6 ms 1240 KB correct
48 Correct 3 ms 860 KB correct
49 Correct 4 ms 1116 KB correct
50 Correct 6 ms 1260 KB correct
51 Correct 9 ms 1628 KB correct
52 Correct 8 ms 1372 KB correct
53 Correct 9 ms 1400 KB correct
54 Correct 10 ms 1628 KB correct
55 Correct 11 ms 1512 KB correct
56 Correct 10 ms 1512 KB correct
57 Correct 10 ms 1628 KB correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB correct
2 Correct 0 ms 424 KB correct
3 Correct 36 ms 4700 KB correct
4 Correct 56 ms 6488 KB correct
5 Correct 57 ms 6492 KB correct
6 Correct 56 ms 6492 KB correct
7 Correct 54 ms 6488 KB correct
8 Correct 54 ms 6488 KB correct
9 Correct 57 ms 6488 KB correct
10 Correct 58 ms 6492 KB correct
11 Correct 57 ms 6492 KB correct
12 Correct 58 ms 6492 KB correct
13 Correct 0 ms 348 KB correct
14 Correct 57 ms 6492 KB correct
15 Correct 58 ms 6492 KB correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB correct
2 Correct 0 ms 348 KB correct
3 Correct 0 ms 348 KB correct
4 Correct 0 ms 348 KB correct
5 Correct 0 ms 348 KB correct
6 Correct 0 ms 348 KB correct
7 Correct 0 ms 348 KB correct
8 Correct 0 ms 348 KB correct
9 Correct 0 ms 348 KB correct
10 Correct 0 ms 348 KB correct
11 Correct 0 ms 348 KB correct
12 Correct 0 ms 348 KB correct
13 Correct 0 ms 412 KB correct
14 Correct 1 ms 600 KB correct
15 Correct 1 ms 604 KB correct
16 Correct 1 ms 604 KB correct
17 Correct 1 ms 604 KB correct
18 Correct 1 ms 604 KB correct
19 Correct 1 ms 604 KB correct
20 Correct 1 ms 604 KB correct
21 Correct 1 ms 604 KB correct
22 Correct 1 ms 604 KB correct
23 Correct 1 ms 604 KB correct
24 Correct 1 ms 604 KB correct
25 Correct 0 ms 348 KB correct
26 Correct 1 ms 604 KB correct
27 Correct 1 ms 604 KB correct
28 Correct 0 ms 604 KB correct
29 Correct 0 ms 604 KB correct
30 Correct 1 ms 604 KB correct
31 Correct 1 ms 604 KB correct
32 Correct 1 ms 604 KB correct
33 Correct 1 ms 604 KB correct
34 Correct 13 ms 2140 KB correct
35 Correct 13 ms 2136 KB correct
36 Correct 11 ms 1884 KB correct
37 Correct 5 ms 1116 KB correct
38 Correct 12 ms 2232 KB correct
39 Correct 12 ms 1884 KB correct
40 Correct 12 ms 1780 KB correct
41 Correct 12 ms 2140 KB correct
42 Correct 12 ms 2176 KB correct
43 Correct 9 ms 1624 KB correct
44 Correct 9 ms 1372 KB correct
45 Correct 10 ms 1636 KB correct
46 Correct 9 ms 1400 KB correct
47 Correct 6 ms 1240 KB correct
48 Correct 3 ms 860 KB correct
49 Correct 4 ms 1116 KB correct
50 Correct 6 ms 1260 KB correct
51 Correct 9 ms 1628 KB correct
52 Correct 8 ms 1372 KB correct
53 Correct 9 ms 1400 KB correct
54 Correct 10 ms 1628 KB correct
55 Correct 11 ms 1512 KB correct
56 Correct 10 ms 1512 KB correct
57 Correct 10 ms 1628 KB correct
58 Correct 0 ms 348 KB correct
59 Correct 0 ms 424 KB correct
60 Correct 36 ms 4700 KB correct
61 Correct 56 ms 6488 KB correct
62 Correct 57 ms 6492 KB correct
63 Correct 56 ms 6492 KB correct
64 Correct 54 ms 6488 KB correct
65 Correct 54 ms 6488 KB correct
66 Correct 57 ms 6488 KB correct
67 Correct 58 ms 6492 KB correct
68 Correct 57 ms 6492 KB correct
69 Correct 58 ms 6492 KB correct
70 Correct 0 ms 348 KB correct
71 Correct 57 ms 6492 KB correct
72 Correct 58 ms 6492 KB correct
73 Correct 0 ms 348 KB correct
74 Correct 57 ms 6480 KB correct
75 Correct 60 ms 6492 KB correct
76 Correct 34 ms 3312 KB correct
77 Correct 59 ms 6708 KB correct
78 Correct 70 ms 6704 KB correct
79 Correct 58 ms 6492 KB correct
80 Correct 58 ms 6404 KB correct
81 Correct 58 ms 5976 KB correct
82 Correct 56 ms 6492 KB correct
83 Correct 47 ms 4188 KB correct
84 Correct 43 ms 4700 KB correct
85 Correct 43 ms 4624 KB correct
86 Correct 35 ms 3420 KB correct
87 Correct 37 ms 3120 KB correct
88 Correct 30 ms 2652 KB correct
89 Correct 30 ms 2392 KB correct
90 Correct 29 ms 2392 KB correct
91 Correct 15 ms 1652 KB correct
92 Correct 14 ms 1372 KB correct
93 Correct 43 ms 4544 KB correct
94 Correct 37 ms 3420 KB correct
95 Correct 37 ms 3420 KB correct
96 Correct 28 ms 2396 KB correct
97 Correct 29 ms 2596 KB correct
98 Correct 32 ms 2908 KB correct
99 Correct 30 ms 2652 KB correct
100 Correct 24 ms 1884 KB correct
101 Correct 11 ms 1624 KB correct
102 Correct 52 ms 4188 KB correct
103 Correct 44 ms 4188 KB correct
104 Correct 45 ms 4208 KB correct
105 Correct 44 ms 4152 KB correct