#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
/*
refs:
https://codeforces.com/blog/entry/53595?#comment-376568
*/
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"
struct DSU {
vector<int> par, rankk, siz;
DSU() {
}
DSU(int n) {
init(n);
}
void init(int n) {
par = vector<int>(n + 1);
rankk = vector<int>(n + 1);
siz = vector<int>(n + 1);
rep(i, n + 1) create(i);
}
void create(int u) {
par[u] = u;
rankk[u] = 0;
siz[u] = 1;
}
int find(int u) {
if (u == par[u]) return u;
else return par[u] = find(par[u]);
}
bool same(int u, int v) {
return find(u) == find(v);
}
void merge(int u, int v) {
u = find(u), v = find(v);
if (u == v) return;
if (rankk[u] == rankk[v]) rankk[u]++;
if (rankk[u] < rankk[v]) swap(u, v);
par[v] = u;
siz[u] += siz[v];
}
};
int f(vector<int> v){
return count_common_roads(v);
}
vector<pii> adj[N];
vector<int> ord;
vector<int> tin(N);
vector<pii> low(N);
vector<bool> vis(N);
int timer = 1;
vector<int> spanning;
vector<pii> par(N);
void dfs1(int u, int p){
vis[u] = 1;
tin[u] = timer++;
low[u] = {tin[u],-1};
for(auto [v,id] : adj[u]){
if(v == p) conts;
if(vis[v]){
amin(low[u],{tin[v],id});
}
else{
spanning.pb(id);
par[v] = {u,id};
dfs1(v,u);
amin(low[u],low[v]);
}
}
ord.pb(u);
}
std::vector<int> find_roads(int n, std::vector<int> U, std::vector<int> V) {
int m = sz(U);
rep(i,m){
int u = U[i], v = V[i];
adj[u].pb({v,i}), adj[v].pb({u,i});
}
dfs1(0,-1);
vector<int> status(m,-1);
int original = f(spanning);
trav(u,ord){
// find the val of the incoming span edge
if(!u) conts;
auto [p,pid] = par[u];
if(low[u].ff > tin[p]){
status[pid] = 1;
conts;
}
if(status[pid] != -1) conts;
int cyc_id = low[u].ss;
int x = U[cyc_id], y = V[cyc_id];
if(tin[x] < tin[y]) swap(x,y);
vector<int> pending;
while(x != y){
auto [p,pid] = par[x];
if(status[pid] != -1){
auto curr = spanning;
curr.erase(find(all(curr),pid));
curr.pb(cyc_id);
int val = f(curr);
int cyc_status = val+status[pid]-original;
if(!count(all(pending),par[u].ss)){
pending.pb(par[u].ss);
}
while(!pending.empty()){
int edge_id = pending.back();
pending.pop_back();
curr = spanning;
curr.erase(find(all(curr),edge_id));
curr.pb(cyc_id);
val = f(curr);
status[edge_id] = original+cyc_status-val;
}
break;
}
x = p;
pending.pb(pid);
}
if(pending.empty()) conts;
// full cycle, none known
pending.pb(cyc_id);
auto curr = spanning;
curr.pb(cyc_id);
map<int,vector<int>> mp;
trav(id,pending){
curr.erase(find(all(curr),id));
int val_without = f(curr);
curr.pb(id);
mp[val_without].pb(id);
}
if(sz(mp) == 1){
// all 0
trav(id,pending){
status[id] = 0;
}
}
else{
assert(sz(mp) == 2);
trav(id,mp.begin()->second){
status[id] = 1;
}
trav(id,(--mp.end())->second){
status[id] = 0;
}
}
}
auto get = [&](vector<int> edges){
// edges form a star at some node u
DSU dsu(n);
trav(id,edges){
dsu.merge(U[id],V[id]);
}
auto curr = edges;
int extra = 0;
trav(id,spanning){
int u = U[id], v = V[id];
if(dsu.same(u,v)) conts;
dsu.merge(u,v);
extra += status[id];
curr.pb(id);
}
int val = f(curr);
return val-extra;
};
// find the status of all non-tree adj edges to u (s.t u < v)
rep(u,n){
vector<int> edges;
for(auto [v,id] : adj[u]){
if(status[id] != -1) conts;
if(v < u) conts;
edges.pb(id);
}
while(!edges.empty()){
if(!get(edges)) break;
int l = 0, r = sz(edges)-1;
int first = -1;
while(l <= r){
int mid = (l+r) >> 1;
vector<int> curr_edges;
rep(i,mid+1) curr_edges.pb(edges[i]);
if(get(curr_edges)){
first = mid;
r = mid-1;
}
else{
l = mid+1;
}
}
assert(first != -1);
vector<int> nxt_edges;
rep(i,sz(edges)){
if(i < first){
status[edges[i]] = 0;
}
else if(i == first){
status[edges[i]] = 1;
}
else{
nxt_edges.pb(edges[i]);
}
}
edges = nxt_edges;
}
}
vector<int> ans;
rep(i,m){
if(status[i] == 1){
ans.pb(i);
}
}
return ans;
}
# |
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 |
1 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 |
1 ms |
348 KB |
correct |
9 |
Correct |
0 ms |
348 KB |
correct |
10 |
Correct |
0 ms |
348 KB |
correct |
11 |
Correct |
1 ms |
344 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 |
1 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 |
1 ms |
348 KB |
correct |
9 |
Correct |
0 ms |
348 KB |
correct |
10 |
Correct |
0 ms |
348 KB |
correct |
11 |
Correct |
1 ms |
344 KB |
correct |
12 |
Correct |
0 ms |
348 KB |
correct |
13 |
Correct |
0 ms |
348 KB |
correct |
14 |
Correct |
1 ms |
348 KB |
correct |
15 |
Correct |
1 ms |
348 KB |
correct |
16 |
Correct |
1 ms |
348 KB |
correct |
17 |
Correct |
1 ms |
348 KB |
correct |
18 |
Correct |
1 ms |
348 KB |
correct |
19 |
Correct |
2 ms |
348 KB |
correct |
20 |
Correct |
1 ms |
348 KB |
correct |
21 |
Correct |
1 ms |
348 KB |
correct |
22 |
Correct |
1 ms |
348 KB |
correct |
23 |
Correct |
1 ms |
348 KB |
correct |
24 |
Correct |
1 ms |
348 KB |
correct |
25 |
Correct |
1 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 |
1 ms |
348 KB |
correct |
30 |
Correct |
1 ms |
348 KB |
correct |
31 |
Correct |
1 ms |
348 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 |
1 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 |
1 ms |
348 KB |
correct |
9 |
Correct |
0 ms |
348 KB |
correct |
10 |
Correct |
0 ms |
348 KB |
correct |
11 |
Correct |
1 ms |
344 KB |
correct |
12 |
Correct |
0 ms |
348 KB |
correct |
13 |
Correct |
0 ms |
348 KB |
correct |
14 |
Correct |
1 ms |
348 KB |
correct |
15 |
Correct |
1 ms |
348 KB |
correct |
16 |
Correct |
1 ms |
348 KB |
correct |
17 |
Correct |
1 ms |
348 KB |
correct |
18 |
Correct |
1 ms |
348 KB |
correct |
19 |
Correct |
2 ms |
348 KB |
correct |
20 |
Correct |
1 ms |
348 KB |
correct |
21 |
Correct |
1 ms |
348 KB |
correct |
22 |
Correct |
1 ms |
348 KB |
correct |
23 |
Correct |
1 ms |
348 KB |
correct |
24 |
Correct |
1 ms |
348 KB |
correct |
25 |
Correct |
1 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 |
1 ms |
348 KB |
correct |
30 |
Correct |
1 ms |
348 KB |
correct |
31 |
Correct |
1 ms |
348 KB |
correct |
32 |
Correct |
1 ms |
348 KB |
correct |
33 |
Correct |
1 ms |
348 KB |
correct |
34 |
Correct |
23 ms |
1628 KB |
correct |
35 |
Correct |
23 ms |
1628 KB |
correct |
36 |
Correct |
20 ms |
1624 KB |
correct |
37 |
Correct |
6 ms |
348 KB |
correct |
38 |
Correct |
23 ms |
1812 KB |
correct |
39 |
Correct |
21 ms |
1628 KB |
correct |
40 |
Correct |
20 ms |
1372 KB |
correct |
41 |
Correct |
22 ms |
1628 KB |
correct |
42 |
Correct |
22 ms |
1628 KB |
correct |
43 |
Correct |
10 ms |
1116 KB |
correct |
44 |
Correct |
9 ms |
860 KB |
correct |
45 |
Correct |
9 ms |
1112 KB |
correct |
46 |
Correct |
9 ms |
860 KB |
correct |
47 |
Correct |
6 ms |
604 KB |
correct |
48 |
Correct |
2 ms |
348 KB |
correct |
49 |
Correct |
4 ms |
348 KB |
correct |
50 |
Correct |
6 ms |
604 KB |
correct |
51 |
Correct |
10 ms |
920 KB |
correct |
52 |
Correct |
9 ms |
860 KB |
correct |
53 |
Correct |
9 ms |
860 KB |
correct |
54 |
Correct |
10 ms |
1116 KB |
correct |
55 |
Correct |
9 ms |
1112 KB |
correct |
56 |
Correct |
9 ms |
1116 KB |
correct |
57 |
Correct |
9 ms |
1116 KB |
correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
correct |
2 |
Correct |
0 ms |
348 KB |
correct |
3 |
Correct |
68 ms |
3888 KB |
correct |
4 |
Correct |
107 ms |
5220 KB |
correct |
5 |
Correct |
111 ms |
5212 KB |
correct |
6 |
Correct |
102 ms |
5212 KB |
correct |
7 |
Correct |
94 ms |
5036 KB |
correct |
8 |
Correct |
96 ms |
5212 KB |
correct |
9 |
Correct |
111 ms |
5196 KB |
correct |
10 |
Correct |
108 ms |
5212 KB |
correct |
11 |
Correct |
108 ms |
5200 KB |
correct |
12 |
Correct |
109 ms |
5400 KB |
correct |
13 |
Correct |
0 ms |
348 KB |
correct |
14 |
Correct |
100 ms |
5212 KB |
correct |
15 |
Correct |
123 ms |
5240 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 |
1 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 |
1 ms |
348 KB |
correct |
9 |
Correct |
0 ms |
348 KB |
correct |
10 |
Correct |
0 ms |
348 KB |
correct |
11 |
Correct |
1 ms |
344 KB |
correct |
12 |
Correct |
0 ms |
348 KB |
correct |
13 |
Correct |
0 ms |
348 KB |
correct |
14 |
Correct |
1 ms |
348 KB |
correct |
15 |
Correct |
1 ms |
348 KB |
correct |
16 |
Correct |
1 ms |
348 KB |
correct |
17 |
Correct |
1 ms |
348 KB |
correct |
18 |
Correct |
1 ms |
348 KB |
correct |
19 |
Correct |
2 ms |
348 KB |
correct |
20 |
Correct |
1 ms |
348 KB |
correct |
21 |
Correct |
1 ms |
348 KB |
correct |
22 |
Correct |
1 ms |
348 KB |
correct |
23 |
Correct |
1 ms |
348 KB |
correct |
24 |
Correct |
1 ms |
348 KB |
correct |
25 |
Correct |
1 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 |
1 ms |
348 KB |
correct |
30 |
Correct |
1 ms |
348 KB |
correct |
31 |
Correct |
1 ms |
348 KB |
correct |
32 |
Correct |
1 ms |
348 KB |
correct |
33 |
Correct |
1 ms |
348 KB |
correct |
34 |
Correct |
23 ms |
1628 KB |
correct |
35 |
Correct |
23 ms |
1628 KB |
correct |
36 |
Correct |
20 ms |
1624 KB |
correct |
37 |
Correct |
6 ms |
348 KB |
correct |
38 |
Correct |
23 ms |
1812 KB |
correct |
39 |
Correct |
21 ms |
1628 KB |
correct |
40 |
Correct |
20 ms |
1372 KB |
correct |
41 |
Correct |
22 ms |
1628 KB |
correct |
42 |
Correct |
22 ms |
1628 KB |
correct |
43 |
Correct |
10 ms |
1116 KB |
correct |
44 |
Correct |
9 ms |
860 KB |
correct |
45 |
Correct |
9 ms |
1112 KB |
correct |
46 |
Correct |
9 ms |
860 KB |
correct |
47 |
Correct |
6 ms |
604 KB |
correct |
48 |
Correct |
2 ms |
348 KB |
correct |
49 |
Correct |
4 ms |
348 KB |
correct |
50 |
Correct |
6 ms |
604 KB |
correct |
51 |
Correct |
10 ms |
920 KB |
correct |
52 |
Correct |
9 ms |
860 KB |
correct |
53 |
Correct |
9 ms |
860 KB |
correct |
54 |
Correct |
10 ms |
1116 KB |
correct |
55 |
Correct |
9 ms |
1112 KB |
correct |
56 |
Correct |
9 ms |
1116 KB |
correct |
57 |
Correct |
9 ms |
1116 KB |
correct |
58 |
Correct |
1 ms |
348 KB |
correct |
59 |
Correct |
0 ms |
348 KB |
correct |
60 |
Correct |
68 ms |
3888 KB |
correct |
61 |
Correct |
107 ms |
5220 KB |
correct |
62 |
Correct |
111 ms |
5212 KB |
correct |
63 |
Correct |
102 ms |
5212 KB |
correct |
64 |
Correct |
94 ms |
5036 KB |
correct |
65 |
Correct |
96 ms |
5212 KB |
correct |
66 |
Correct |
111 ms |
5196 KB |
correct |
67 |
Correct |
108 ms |
5212 KB |
correct |
68 |
Correct |
108 ms |
5200 KB |
correct |
69 |
Correct |
109 ms |
5400 KB |
correct |
70 |
Correct |
0 ms |
348 KB |
correct |
71 |
Correct |
100 ms |
5212 KB |
correct |
72 |
Correct |
123 ms |
5240 KB |
correct |
73 |
Correct |
1 ms |
344 KB |
correct |
74 |
Correct |
107 ms |
6172 KB |
correct |
75 |
Correct |
104 ms |
6036 KB |
correct |
76 |
Correct |
55 ms |
2648 KB |
correct |
77 |
Correct |
108 ms |
5972 KB |
correct |
78 |
Correct |
109 ms |
6148 KB |
correct |
79 |
Correct |
108 ms |
5980 KB |
correct |
80 |
Correct |
118 ms |
6044 KB |
correct |
81 |
Correct |
91 ms |
5464 KB |
correct |
82 |
Correct |
108 ms |
5904 KB |
correct |
83 |
Correct |
81 ms |
3420 KB |
correct |
84 |
Correct |
46 ms |
4188 KB |
correct |
85 |
Correct |
45 ms |
3928 KB |
correct |
86 |
Correct |
37 ms |
2648 KB |
correct |
87 |
Correct |
34 ms |
2136 KB |
correct |
88 |
Correct |
29 ms |
1628 KB |
correct |
89 |
Correct |
30 ms |
1628 KB |
correct |
90 |
Correct |
31 ms |
1628 KB |
correct |
91 |
Correct |
14 ms |
600 KB |
correct |
92 |
Correct |
7 ms |
348 KB |
correct |
93 |
Correct |
53 ms |
4108 KB |
correct |
94 |
Correct |
36 ms |
2648 KB |
correct |
95 |
Correct |
38 ms |
2812 KB |
correct |
96 |
Correct |
29 ms |
1624 KB |
correct |
97 |
Correct |
32 ms |
1760 KB |
correct |
98 |
Correct |
32 ms |
2396 KB |
correct |
99 |
Correct |
35 ms |
1628 KB |
correct |
100 |
Correct |
20 ms |
604 KB |
correct |
101 |
Correct |
9 ms |
344 KB |
correct |
102 |
Correct |
38 ms |
3164 KB |
correct |
103 |
Correct |
45 ms |
3772 KB |
correct |
104 |
Correct |
38 ms |
3340 KB |
correct |
105 |
Correct |
38 ms |
3344 KB |
correct |