#pragma GCC optimize("Ofast")
#include "simurgh.h"
#include <bits/stdc++.h>
using namespace std;
vector<int> find_roads(int n, vector<int> u, vector<int> v){
int m = u.size();
set<int> graph;
vector<set<pair<int,int>>> adj(n);
vector<bool> confirmed(m);
function<bool(int,int,int)> finder = [&](int x,int p,int tar) {
if(x==tar)return true;
for(auto[v,idx]:adj[x])if(v!=p)if(finder(v,x,tar))return true;
return false;
};
auto differential = [&](int x){
stack<int> edges;
function<bool(int,int)> dfs = [&](int xx,int p) {
if(xx==v[x])return true;
for(auto[v,idx]:adj[xx])if(v!=p){
edges.emplace(idx);
if(dfs(v,xx))return true;
edges.pop();
}
return false;
};
dfs(u[x],-1);
if(edges.empty()) {
graph.insert(x);
adj[u[x]].emplace(v[x],x);
adj[v[x]].emplace(u[x],x);
}
vector<int> finaledges;
while(!edges.empty()) {
finaledges.emplace_back(edges.top());
edges.pop();
}
vector<int> sameas;
bool included = false;
for(int&i:finaledges) {
if(confirmed[i])continue;
auto base = count_common_roads(vector(graph.begin(),graph.end()));
graph.erase(i);
graph.insert(x);
auto diff = count_common_roads(vector(graph.begin(),graph.end()));
graph.erase(x);
graph.insert(i);
if(diff==base) {
sameas.emplace_back(i);
continue;
}
if(diff>base) {
included=true;
graph.erase(i);
adj[u[i]].erase({v[i],i});
adj[v[i]].erase({u[i],i});
}
break;
}
if(included) {
graph.insert(x);
adj[u[x]].emplace(v[x],x);
adj[v[x]].emplace(u[x],x);
confirmed[x]=true;
for(int&i:sameas)confirmed[i]=true;
} else {
for(int&i:sameas) {
graph.erase(i);
adj[u[i]].erase({v[i],i});
adj[v[i]].erase({u[i],i});
}
}
};
vector<bool> visited(m);
for(int i=0;i<m;i++) {
if(graph.size()!=n-1) {
for(int j=i;j<m;j++) {
if(visited[j])continue;
if(finder(u[j],-1,v[j])==false) {
visited[j]=true;
graph.insert(j);
adj[u[j]].emplace(v[j],j);
adj[v[j]].emplace(u[j],j);
if(graph.size()==n-1)break;
}
}
}
if(visited[i])continue;
differential(i);
}
return vector(graph.begin(), graph.end());
}
Compilation message
simurgh.cpp: In function 'std::vector<int> find_roads(int, std::vector<int>, std::vector<int>)':
simurgh.cpp:76:18: warning: comparison of integer expressions of different signedness: 'std::set<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
76 | if(graph.size()!=n-1) {
| ~~~~~~~~~~~~^~~~~
simurgh.cpp:84:21: warning: comparison of integer expressions of different signedness: 'std::set<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
84 | if(graph.size()==n-1)break;
| ~~~~~~~~~~~~^~~~~
# |
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 |
396 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 |
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 |
396 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 |
5 ms |
348 KB |
correct |
15 |
Correct |
4 ms |
348 KB |
correct |
16 |
Correct |
4 ms |
348 KB |
correct |
17 |
Correct |
3 ms |
460 KB |
correct |
18 |
Correct |
1 ms |
348 KB |
correct |
19 |
Correct |
3 ms |
460 KB |
correct |
20 |
Correct |
3 ms |
348 KB |
correct |
21 |
Correct |
4 ms |
348 KB |
correct |
22 |
Correct |
2 ms |
344 KB |
correct |
23 |
Correct |
1 ms |
348 KB |
correct |
24 |
Correct |
2 ms |
348 KB |
correct |
25 |
Correct |
0 ms |
348 KB |
correct |
26 |
Correct |
1 ms |
348 KB |
correct |
27 |
Correct |
1 ms |
348 KB |
correct |
28 |
Correct |
1 ms |
348 KB |
correct |
29 |
Correct |
0 ms |
348 KB |
correct |
30 |
Correct |
2 ms |
348 KB |
correct |
31 |
Correct |
1 ms |
448 KB |
correct |
32 |
Correct |
1 ms |
348 KB |
correct |
33 |
Correct |
1 ms |
348 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 |
396 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 |
5 ms |
348 KB |
correct |
15 |
Correct |
4 ms |
348 KB |
correct |
16 |
Correct |
4 ms |
348 KB |
correct |
17 |
Correct |
3 ms |
460 KB |
correct |
18 |
Correct |
1 ms |
348 KB |
correct |
19 |
Correct |
3 ms |
460 KB |
correct |
20 |
Correct |
3 ms |
348 KB |
correct |
21 |
Correct |
4 ms |
348 KB |
correct |
22 |
Correct |
2 ms |
344 KB |
correct |
23 |
Correct |
1 ms |
348 KB |
correct |
24 |
Correct |
2 ms |
348 KB |
correct |
25 |
Correct |
0 ms |
348 KB |
correct |
26 |
Correct |
1 ms |
348 KB |
correct |
27 |
Correct |
1 ms |
348 KB |
correct |
28 |
Correct |
1 ms |
348 KB |
correct |
29 |
Correct |
0 ms |
348 KB |
correct |
30 |
Correct |
2 ms |
348 KB |
correct |
31 |
Correct |
1 ms |
448 KB |
correct |
32 |
Correct |
1 ms |
348 KB |
correct |
33 |
Correct |
1 ms |
348 KB |
correct |
34 |
Incorrect |
466 ms |
928 KB |
WA in grader: NO |
35 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
correct |
2 |
Correct |
0 ms |
348 KB |
correct |
3 |
Incorrect |
444 ms |
1580 KB |
WA in grader: NO |
4 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
396 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 |
5 ms |
348 KB |
correct |
15 |
Correct |
4 ms |
348 KB |
correct |
16 |
Correct |
4 ms |
348 KB |
correct |
17 |
Correct |
3 ms |
460 KB |
correct |
18 |
Correct |
1 ms |
348 KB |
correct |
19 |
Correct |
3 ms |
460 KB |
correct |
20 |
Correct |
3 ms |
348 KB |
correct |
21 |
Correct |
4 ms |
348 KB |
correct |
22 |
Correct |
2 ms |
344 KB |
correct |
23 |
Correct |
1 ms |
348 KB |
correct |
24 |
Correct |
2 ms |
348 KB |
correct |
25 |
Correct |
0 ms |
348 KB |
correct |
26 |
Correct |
1 ms |
348 KB |
correct |
27 |
Correct |
1 ms |
348 KB |
correct |
28 |
Correct |
1 ms |
348 KB |
correct |
29 |
Correct |
0 ms |
348 KB |
correct |
30 |
Correct |
2 ms |
348 KB |
correct |
31 |
Correct |
1 ms |
448 KB |
correct |
32 |
Correct |
1 ms |
348 KB |
correct |
33 |
Correct |
1 ms |
348 KB |
correct |
34 |
Incorrect |
466 ms |
928 KB |
WA in grader: NO |
35 |
Halted |
0 ms |
0 KB |
- |