Submission #1051193

# Submission time Handle Problem Language Result Execution time Memory
1051193 2024-08-09T23:07:06 Z boyliguanhan Simurgh (IOI17_simurgh) C++17
100 / 100
61 ms 6716 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 1 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 344 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 348 KB correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 344 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 348 KB correct
14 Correct 1 ms 604 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 0 ms 604 KB correct
19 Correct 1 ms 604 KB correct
20 Correct 1 ms 480 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 0 ms 604 KB correct
27 Correct 1 ms 600 KB correct
28 Correct 1 ms 604 KB correct
29 Correct 0 ms 472 KB correct
30 Correct 1 ms 604 KB correct
31 Correct 1 ms 604 KB correct
32 Correct 1 ms 856 KB correct
33 Correct 1 ms 604 KB correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 344 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 348 KB correct
14 Correct 1 ms 604 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 0 ms 604 KB correct
19 Correct 1 ms 604 KB correct
20 Correct 1 ms 480 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 0 ms 604 KB correct
27 Correct 1 ms 600 KB correct
28 Correct 1 ms 604 KB correct
29 Correct 0 ms 472 KB correct
30 Correct 1 ms 604 KB correct
31 Correct 1 ms 604 KB correct
32 Correct 1 ms 856 KB correct
33 Correct 1 ms 604 KB correct
34 Correct 12 ms 2140 KB correct
35 Correct 13 ms 2140 KB correct
36 Correct 14 ms 1944 KB correct
37 Correct 5 ms 996 KB correct
38 Correct 13 ms 2240 KB correct
39 Correct 12 ms 1884 KB correct
40 Correct 12 ms 1884 KB correct
41 Correct 12 ms 2136 KB correct
42 Correct 12 ms 2172 KB correct
43 Correct 9 ms 1628 KB correct
44 Correct 8 ms 1548 KB correct
45 Correct 9 ms 1628 KB correct
46 Correct 11 ms 1368 KB correct
47 Correct 6 ms 1112 KB correct
48 Correct 3 ms 860 KB correct
49 Correct 6 ms 1112 KB correct
50 Correct 7 ms 1116 KB correct
51 Correct 11 ms 1516 KB correct
52 Correct 8 ms 1372 KB correct
53 Correct 8 ms 1520 KB correct
54 Correct 11 ms 1880 KB correct
55 Correct 9 ms 1672 KB correct
56 Correct 10 ms 1628 KB correct
57 Correct 10 ms 1676 KB correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB correct
2 Correct 0 ms 348 KB correct
3 Correct 36 ms 4700 KB correct
4 Correct 55 ms 6716 KB correct
5 Correct 57 ms 6488 KB correct
6 Correct 56 ms 6704 KB correct
7 Correct 60 ms 6492 KB correct
8 Correct 55 ms 6492 KB correct
9 Correct 55 ms 6568 KB correct
10 Correct 55 ms 6492 KB correct
11 Correct 56 ms 6492 KB correct
12 Correct 61 ms 6688 KB correct
13 Correct 0 ms 348 KB correct
14 Correct 59 ms 6704 KB correct
15 Correct 57 ms 6492 KB correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 344 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 348 KB correct
14 Correct 1 ms 604 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 0 ms 604 KB correct
19 Correct 1 ms 604 KB correct
20 Correct 1 ms 480 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 0 ms 604 KB correct
27 Correct 1 ms 600 KB correct
28 Correct 1 ms 604 KB correct
29 Correct 0 ms 472 KB correct
30 Correct 1 ms 604 KB correct
31 Correct 1 ms 604 KB correct
32 Correct 1 ms 856 KB correct
33 Correct 1 ms 604 KB correct
34 Correct 12 ms 2140 KB correct
35 Correct 13 ms 2140 KB correct
36 Correct 14 ms 1944 KB correct
37 Correct 5 ms 996 KB correct
38 Correct 13 ms 2240 KB correct
39 Correct 12 ms 1884 KB correct
40 Correct 12 ms 1884 KB correct
41 Correct 12 ms 2136 KB correct
42 Correct 12 ms 2172 KB correct
43 Correct 9 ms 1628 KB correct
44 Correct 8 ms 1548 KB correct
45 Correct 9 ms 1628 KB correct
46 Correct 11 ms 1368 KB correct
47 Correct 6 ms 1112 KB correct
48 Correct 3 ms 860 KB correct
49 Correct 6 ms 1112 KB correct
50 Correct 7 ms 1116 KB correct
51 Correct 11 ms 1516 KB correct
52 Correct 8 ms 1372 KB correct
53 Correct 8 ms 1520 KB correct
54 Correct 11 ms 1880 KB correct
55 Correct 9 ms 1672 KB correct
56 Correct 10 ms 1628 KB correct
57 Correct 10 ms 1676 KB correct
58 Correct 0 ms 348 KB correct
59 Correct 0 ms 348 KB correct
60 Correct 36 ms 4700 KB correct
61 Correct 55 ms 6716 KB correct
62 Correct 57 ms 6488 KB correct
63 Correct 56 ms 6704 KB correct
64 Correct 60 ms 6492 KB correct
65 Correct 55 ms 6492 KB correct
66 Correct 55 ms 6568 KB correct
67 Correct 55 ms 6492 KB correct
68 Correct 56 ms 6492 KB correct
69 Correct 61 ms 6688 KB correct
70 Correct 0 ms 348 KB correct
71 Correct 59 ms 6704 KB correct
72 Correct 57 ms 6492 KB correct
73 Correct 0 ms 344 KB correct
74 Correct 56 ms 6700 KB correct
75 Correct 56 ms 6488 KB correct
76 Correct 36 ms 3160 KB correct
77 Correct 61 ms 6492 KB correct
78 Correct 56 ms 6492 KB correct
79 Correct 56 ms 6488 KB correct
80 Correct 56 ms 6408 KB correct
81 Correct 55 ms 6228 KB correct
82 Correct 61 ms 6488 KB correct
83 Correct 47 ms 4352 KB correct
84 Correct 47 ms 4700 KB correct
85 Correct 41 ms 4440 KB correct
86 Correct 35 ms 3420 KB correct
87 Correct 34 ms 3092 KB correct
88 Correct 30 ms 2648 KB correct
89 Correct 29 ms 2392 KB correct
90 Correct 29 ms 2396 KB correct
91 Correct 15 ms 1628 KB correct
92 Correct 10 ms 1580 KB correct
93 Correct 44 ms 4628 KB correct
94 Correct 35 ms 3416 KB correct
95 Correct 37 ms 3420 KB correct
96 Correct 29 ms 2392 KB correct
97 Correct 29 ms 2624 KB correct
98 Correct 32 ms 2908 KB correct
99 Correct 30 ms 2652 KB correct
100 Correct 20 ms 1884 KB correct
101 Correct 10 ms 1724 KB correct
102 Correct 49 ms 4188 KB correct
103 Correct 43 ms 4184 KB correct
104 Correct 47 ms 4188 KB correct
105 Correct 45 ms 4212 KB correct