#include "simurgh.h"
#include<bits/stdc++.h>
using namespace std;
vector<int>R;
int query(){
return count_common_roads(R);
}
vector<int>U,V;
vector<int>subtree[501];
vector<int>AADJ[10010],adj[501];
int indexofr[501][501];
bitset<501>vis;
int N_;
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;
void gendfstree(int n){
vis[n]=1;
subtree[n].push_back(n);
for(auto i:AADJ[n]){
if(vis[i])continue;
gendfstree(i);
for(auto j:subtree[i])
subtree[n].push_back(j);
adj[i].push_back(n);
adj[n].push_back(i);
}
}
void mergethese(vector<int>v){
tis.reset();
set<int>st,st2;
for(int i=0;i<N_;i++)
st2.insert(i);
for(auto i:v)
st.insert(i),st2.erase(i);
for(int i=0;i<U.size();i++){
int a=U[i],b=V[i];
if(!st.count(a)||!st.count(b))
continue;
if(tis.merge(a,b))
R.push_back(i);
}
tis.reset();
for(int i=0;i<U.size();i++){
int a=U[i],b=V[i];
if(!st2.count(a)||!st2.count(b))
continue;
if(tis.merge(a,b))
R.push_back(i);
}
}
void specialcheck(int n){
}
vector<int>ans;
void findstuff(int n,int p){
for(auto i:adj[n])
if(i-p)findstuff(i,n);
if(!n)return;
vector<int>R2=R;
R.clear();
mergethese(subtree[n]);
vector<int>specrod;
for(auto i:AADJ[n])
if(subtree[i].size()>subtree[n].size())
specrod.push_back(indexofr[i][n]);
map<int,vector<int>>mp;
for(auto i:specrod){
R.push_back(i);
mp[query()].push_back(i);
R.pop_back();
}
vector<int>C=prev(mp.end())->second;
if(mp.size()==2){
for(auto i:C)
TIS2.merge(U[i],V[i]),
ans.push_back(i);
} else if(TIS2.merge(U[C[0]],V[C[0]]))
for(auto i:C)
TIS2.merge(U[i],V[i]),
ans.push_back(i);
}
std::vector<int> find_roads(int n, std::vector<int> u, std::vector<int> v) {
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;
gendfstree(0);
findstuff(0,0);
return ans;
}
Compilation message
simurgh.cpp: In member function 'bool dsu::merge(int, int)':
simurgh.cpp:22:22: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
22 | return par[a]=b;
| ~~~~~~^~
simurgh.cpp: In function 'void mergethese(std::vector<int>)':
simurgh.cpp:47:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | for(int i=0;i<U.size();i++){
| ~^~~~~~~~~
simurgh.cpp:55:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
55 | for(int i=0;i<U.size();i++){
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
604 KB |
correct |
2 |
Incorrect |
0 ms |
680 KB |
WA in grader: NO |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
604 KB |
correct |
2 |
Incorrect |
0 ms |
680 KB |
WA in grader: NO |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
604 KB |
correct |
2 |
Incorrect |
0 ms |
680 KB |
WA in grader: NO |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
604 KB |
correct |
2 |
Incorrect |
0 ms |
604 KB |
WA in grader: NO |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
604 KB |
correct |
2 |
Incorrect |
0 ms |
680 KB |
WA in grader: NO |
3 |
Halted |
0 ms |
0 KB |
- |