#include <bits/stdc++.h>
#include "simurgh.h"
using namespace std;
vector<pair<int,int>> graph[501],graph2[501*501];
int n,q,pre,h[501],par[501],par2[501];
bool seen[501],seen2[501*501];
set<int> used,asked;
vector<int> tp[2];
int c(){
if(used.size() != n-1){
return -1;
}
q++;
assert(q <= 29000);
vector<int> v;
for(auto i : used){
v.push_back(i);
}
return count_common_roads(v);
}
void dfs(int curr){
seen[curr] = 1;
for(auto i : graph[curr]){
if(seen[i.first]){
continue;
}
h[i.first] = h[curr]+1;
used.insert(i.second);
par[i.first] = curr , par2[i.first] = i.second;
dfs(i.first);
}
}
void ask(int a , int b){
if(asked.count(b) && asked.count(a)){
return ;
}
used.erase(a),used.insert(b);
int val = c();
if(pre == val){
graph2[a].push_back({b,0});
graph2[b].push_back({a,0});
}else{
graph2[a].push_back({b,1});
graph2[b].push_back({a,1});
}
used.erase(b),used.insert(a);
asked.insert(a),asked.insert(b);
}
void dfs2(int curr , int val){
if(used.count(curr)){
used.erase(curr);
}
seen2[curr] = 1;
tp[val].push_back(curr);
for(auto i : graph2[curr]){
if(seen2[i.first]){
continue;
}
dfs2(i.first,val^i.second);
}
}
vector<int> find_roads(int N , vector<int> from , vector<int> to){
n = N;
for(int i = 0 ; i < from.size() ; i += 1){
int a = from[i] , b = to[i];
graph[a].push_back({b,i}),graph[b].push_back({a,i});
}
dfs(0);
pre = c();
for(int i = 0 ; i < from.size() ; i += 1){
if(used.count(i)){
continue;
}
asked.insert(i);
int a = from[i] , b = to[i];
if(h[a] < h[b]){
swap(a,b);
}
ask(par2[a],i);
a = par[a];
if(h[a] < h[b]){
swap(a,b);
}
while(h[a] > h[b]){
ask(par2[a],i);
a = par[a];
}
while(a != b){
ask(par2[a],i),ask(par2[b],i);
a = par[a] , b = par[b];
}
}
vector<int> v;
for(auto i : used){
v.push_back(i);
}
for(auto i : v){
if(seen2[i] || !asked.count(i)){
continue;
}
tp[0].clear(),tp[1].clear();
dfs2(i,0);
for(auto j : tp[0]){
used.insert(j);
}
int val1 = c();
for(auto j : tp[0]){
used.erase(j);
}
for(auto j : tp[1]){
used.insert(j);
}
int val2 = c();
if(val2 > val1){
continue;
}
for(auto j : tp[1]){
used.erase(j);
}
for(auto j : tp[0]){
used.insert(j);
}
}
v.clear();
for(auto i : used){
v.push_back(i);
}
return v;
}
Compilation message
simurgh.cpp: In function 'int c()':
simurgh.cpp:11:17: warning: comparison of integer expressions of different signedness: 'std::set<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
11 | if(used.size() != n-1){
| ~~~~~~~~~~~~^~~~~~
simurgh.cpp: In function 'std::vector<int> find_roads(int, std::vector<int>, std::vector<int>)':
simurgh.cpp:69:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
69 | for(int i = 0 ; i < from.size() ; i += 1){
| ~~^~~~~~~~~~~~~
simurgh.cpp:75:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
75 | for(int i = 0 ; i < from.size() ; i += 1){
| ~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
6092 KB |
WA in grader: NO |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
6092 KB |
WA in grader: NO |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
6092 KB |
WA in grader: NO |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
6092 KB |
correct |
2 |
Incorrect |
4 ms |
6220 KB |
WA in grader: NO |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
6092 KB |
WA in grader: NO |
2 |
Halted |
0 ms |
0 KB |
- |