#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define sz(a) (int)a.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define conts continue
#define ceil2(x,y) ((x+y-1)/(y))
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl
#define rep(i,n) for(int i = 0; i < n; ++i)
#define rep1(i,n) for(int i = 1; i <= n; ++i)
#define rev(i,s,e) for(int i = s; i >= e; --i)
#define trav(i,a) for(auto &i : a)
template<typename T>
void amin(T &a, T b) {
a = min(a,b);
}
template<typename T>
void amax(T &a, T b) {
a = max(a,b);
}
#ifdef LOCAL
#include "debug.h"
#else
#define debug(x) 42
#endif
/*
*/
const int MOD = 1e9 + 7;
const int N = 500 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;
#include "simurgh.h"
vector<pii> adj[N];
vector<bool> vis(N);
vector<int> tin(N), tout(N), low(N);
vector<array<int,3>> back_edges;
vector<pii> par(N);
int timer;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void dfs1(int u, int p){
vis[u] = 1;
tin[u] = low[u] = timer++;
shuffle(all(adj[u]),rng);
for(auto [v,id] : adj[u]){
if(v == p) conts;
if(vis[v]){
if(tin[u] > tin[v]){
back_edges.pb({u,v,id});
}
amin(low[u],tin[v]);
}
else{
par[v] = {u,id};
dfs1(v,u);
amin(low[u],low[v]);
}
}
tout[u] = timer++;
}
std::vector<int> find_roads(int n, std::vector<int> U, std::vector<int> V) {
int m = sz(U);
vector<int> status(m);
while(count(all(status),1) < n-1){
rep(i,n){
adj[i].clear();
vis[i] = 0;
}
back_edges.clear();
timer = 0;
rep(i,m){
if(status[i] != -1){
int u = U[i], v = V[i];
adj[u].pb({v,i}), adj[v].pb({u,i});
}
}
dfs1(0,-1);
if(back_edges.empty()){
rep(i,m){
if(status[i] == 0){
status[i] = 1;
}
}
break;
}
vector<int> spanning;
rep1(i,n-1){
spanning.pb(par[i].ss);
}
vector<array<int,3>> good, pending;
trav(ar,back_edges){
if(status[ar[2]] == 1){
good.pb(ar);
}
else{
pending.pb(ar);
}
}
back_edges = good;
trav(ar,pending){
back_edges.pb(ar);
}
int original = count_common_roads(spanning);
for(auto [u,v,id] : back_edges){
if(status[id] == 1){
// back edge is definitely good
while(u != v){
auto [p,pid] = par[u];
if(status[pid] == 1){
u = p;
conts;
}
spanning.erase(find(all(spanning),pid));
spanning.pb(id);
int curr = count_common_roads(spanning);
spanning.pop_back();
spanning.pb(pid);
if(curr < original){
assert(0);
}
else if(curr == original){
status[pid] = 1;
}
else{
status[pid] = -1;
}
u = p;
}
}
else{
// back edge may/may not be good
int bad_cnt = 0;
vector<int> same_edges;
same_edges.pb(id);
while(u != v){
auto [p,pid] = par[u];
if(status[pid] == 1){
u = p;
conts;
}
spanning.erase(find(all(spanning),pid));
spanning.pb(id);
int curr = count_common_roads(spanning);
spanning.pop_back();
spanning.pb(pid);
if(curr < original){
status[pid] = 1;
}
else if(curr == original){
same_edges.pb(pid);
}
else{
status[pid] = -1;
bad_cnt++;
}
u = p;
}
if(bad_cnt){
trav(edge_id,same_edges){
status[edge_id] = 1;
}
}
else{
trav(edge_id,same_edges){
status[edge_id] = -1;
}
break;
}
}
}
}
vector<int> ans;
rep(i,m){
if(status[i] == 1){
ans.pb(i);
}
}
debug(ans);
return ans;
}
Compilation message
simurgh.cpp: In function 'std::vector<int> find_roads(int, std::vector<int>, std::vector<int>)':
simurgh.cpp:46:18: warning: statement has no effect [-Wunused-value]
46 | #define debug(x) 42
| ^~
simurgh.cpp:231:2: note: in expansion of macro 'debug'
231 | debug(ans);
| ^~~~~
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
348 KB |
correct |
7 |
Correct |
0 ms |
348 KB |
correct |
8 |
Correct |
0 ms |
600 KB |
correct |
9 |
Correct |
0 ms |
348 KB |
correct |
10 |
Correct |
1 ms |
344 KB |
correct |
11 |
Correct |
0 ms |
348 KB |
correct |
12 |
Correct |
1 ms |
348 KB |
correct |
13 |
Correct |
0 ms |
348 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
348 KB |
correct |
7 |
Correct |
0 ms |
348 KB |
correct |
8 |
Correct |
0 ms |
600 KB |
correct |
9 |
Correct |
0 ms |
348 KB |
correct |
10 |
Correct |
1 ms |
344 KB |
correct |
11 |
Correct |
0 ms |
348 KB |
correct |
12 |
Correct |
1 ms |
348 KB |
correct |
13 |
Correct |
0 ms |
348 KB |
correct |
14 |
Correct |
7 ms |
348 KB |
correct |
15 |
Correct |
6 ms |
584 KB |
correct |
16 |
Correct |
6 ms |
348 KB |
correct |
17 |
Correct |
6 ms |
556 KB |
correct |
18 |
Correct |
1 ms |
348 KB |
correct |
19 |
Correct |
5 ms |
348 KB |
correct |
20 |
Correct |
4 ms |
348 KB |
correct |
21 |
Correct |
4 ms |
348 KB |
correct |
22 |
Correct |
2 ms |
348 KB |
correct |
23 |
Correct |
3 ms |
348 KB |
correct |
24 |
Correct |
2 ms |
348 KB |
correct |
25 |
Correct |
0 ms |
344 KB |
correct |
26 |
Correct |
2 ms |
348 KB |
correct |
27 |
Correct |
2 ms |
348 KB |
correct |
28 |
Correct |
1 ms |
348 KB |
correct |
29 |
Correct |
1 ms |
348 KB |
correct |
30 |
Correct |
3 ms |
348 KB |
correct |
31 |
Correct |
3 ms |
344 KB |
correct |
32 |
Correct |
4 ms |
348 KB |
correct |
33 |
Correct |
3 ms |
348 KB |
correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
348 KB |
correct |
7 |
Correct |
0 ms |
348 KB |
correct |
8 |
Correct |
0 ms |
600 KB |
correct |
9 |
Correct |
0 ms |
348 KB |
correct |
10 |
Correct |
1 ms |
344 KB |
correct |
11 |
Correct |
0 ms |
348 KB |
correct |
12 |
Correct |
1 ms |
348 KB |
correct |
13 |
Correct |
0 ms |
348 KB |
correct |
14 |
Correct |
7 ms |
348 KB |
correct |
15 |
Correct |
6 ms |
584 KB |
correct |
16 |
Correct |
6 ms |
348 KB |
correct |
17 |
Correct |
6 ms |
556 KB |
correct |
18 |
Correct |
1 ms |
348 KB |
correct |
19 |
Correct |
5 ms |
348 KB |
correct |
20 |
Correct |
4 ms |
348 KB |
correct |
21 |
Correct |
4 ms |
348 KB |
correct |
22 |
Correct |
2 ms |
348 KB |
correct |
23 |
Correct |
3 ms |
348 KB |
correct |
24 |
Correct |
2 ms |
348 KB |
correct |
25 |
Correct |
0 ms |
344 KB |
correct |
26 |
Correct |
2 ms |
348 KB |
correct |
27 |
Correct |
2 ms |
348 KB |
correct |
28 |
Correct |
1 ms |
348 KB |
correct |
29 |
Correct |
1 ms |
348 KB |
correct |
30 |
Correct |
3 ms |
348 KB |
correct |
31 |
Correct |
3 ms |
344 KB |
correct |
32 |
Correct |
4 ms |
348 KB |
correct |
33 |
Correct |
3 ms |
348 KB |
correct |
34 |
Incorrect |
113 ms |
2700 KB |
WA in grader: NO |
35 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
correct |
2 |
Correct |
0 ms |
344 KB |
correct |
3 |
Incorrect |
259 ms |
7332 KB |
WA in grader: NO |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
348 KB |
correct |
7 |
Correct |
0 ms |
348 KB |
correct |
8 |
Correct |
0 ms |
600 KB |
correct |
9 |
Correct |
0 ms |
348 KB |
correct |
10 |
Correct |
1 ms |
344 KB |
correct |
11 |
Correct |
0 ms |
348 KB |
correct |
12 |
Correct |
1 ms |
348 KB |
correct |
13 |
Correct |
0 ms |
348 KB |
correct |
14 |
Correct |
7 ms |
348 KB |
correct |
15 |
Correct |
6 ms |
584 KB |
correct |
16 |
Correct |
6 ms |
348 KB |
correct |
17 |
Correct |
6 ms |
556 KB |
correct |
18 |
Correct |
1 ms |
348 KB |
correct |
19 |
Correct |
5 ms |
348 KB |
correct |
20 |
Correct |
4 ms |
348 KB |
correct |
21 |
Correct |
4 ms |
348 KB |
correct |
22 |
Correct |
2 ms |
348 KB |
correct |
23 |
Correct |
3 ms |
348 KB |
correct |
24 |
Correct |
2 ms |
348 KB |
correct |
25 |
Correct |
0 ms |
344 KB |
correct |
26 |
Correct |
2 ms |
348 KB |
correct |
27 |
Correct |
2 ms |
348 KB |
correct |
28 |
Correct |
1 ms |
348 KB |
correct |
29 |
Correct |
1 ms |
348 KB |
correct |
30 |
Correct |
3 ms |
348 KB |
correct |
31 |
Correct |
3 ms |
344 KB |
correct |
32 |
Correct |
4 ms |
348 KB |
correct |
33 |
Correct |
3 ms |
348 KB |
correct |
34 |
Incorrect |
113 ms |
2700 KB |
WA in grader: NO |
35 |
Halted |
0 ms |
0 KB |
- |