Submission #934497

# Submission time Handle Problem Language Result Execution time Memory
934497 2024-02-27T13:06:44 Z GrindMachine Simurgh (IOI17_simurgh) C++17
51 / 100
204 ms 7308 KB
#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(5);

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] == 0){
				int u = U[i], v = V[i];
				adj[u].pb({v,i}), adj[v].pb({u,i});
			}
		}
 	
 		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;
					}
					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:242:2: note: in expansion of macro 'debug'
  242 |  debug(ans);
      |  ^~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 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 348 KB correct
10 Correct 0 ms 344 KB correct
11 Correct 0 ms 600 KB correct
12 Correct 0 ms 348 KB correct
13 Correct 0 ms 348 KB correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 348 KB correct
10 Correct 0 ms 344 KB correct
11 Correct 0 ms 600 KB correct
12 Correct 0 ms 348 KB correct
13 Correct 0 ms 348 KB correct
14 Correct 2 ms 348 KB correct
15 Correct 2 ms 456 KB correct
16 Correct 2 ms 348 KB correct
17 Correct 2 ms 348 KB correct
18 Correct 1 ms 344 KB correct
19 Correct 2 ms 348 KB correct
20 Correct 2 ms 348 KB correct
21 Correct 2 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 344 KB correct
30 Correct 1 ms 552 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 1 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 348 KB correct
10 Correct 0 ms 344 KB correct
11 Correct 0 ms 600 KB correct
12 Correct 0 ms 348 KB correct
13 Correct 0 ms 348 KB correct
14 Correct 2 ms 348 KB correct
15 Correct 2 ms 456 KB correct
16 Correct 2 ms 348 KB correct
17 Correct 2 ms 348 KB correct
18 Correct 1 ms 344 KB correct
19 Correct 2 ms 348 KB correct
20 Correct 2 ms 348 KB correct
21 Correct 2 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 344 KB correct
30 Correct 1 ms 552 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 145 ms 2668 KB correct
35 Correct 149 ms 2612 KB correct
36 Correct 98 ms 2244 KB correct
37 Correct 4 ms 348 KB correct
38 Correct 140 ms 2664 KB correct
39 Correct 113 ms 2504 KB correct
40 Correct 94 ms 2264 KB correct
41 Correct 161 ms 2676 KB correct
42 Correct 153 ms 2884 KB correct
43 Correct 72 ms 1892 KB correct
44 Correct 57 ms 1544 KB correct
45 Correct 63 ms 1676 KB correct
46 Correct 48 ms 1452 KB correct
47 Correct 19 ms 856 KB correct
48 Correct 2 ms 344 KB correct
49 Correct 3 ms 604 KB correct
50 Correct 7 ms 1000 KB correct
51 Correct 47 ms 1668 KB correct
52 Correct 23 ms 1524 KB correct
53 Correct 26 ms 1460 KB correct
54 Correct 23 ms 1892 KB correct
55 Correct 55 ms 1676 KB correct
56 Correct 57 ms 1688 KB correct
57 Correct 14 ms 1684 KB correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB correct
2 Correct 0 ms 348 KB correct
3 Incorrect 204 ms 7308 KB WA in grader: NO
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 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 348 KB correct
10 Correct 0 ms 344 KB correct
11 Correct 0 ms 600 KB correct
12 Correct 0 ms 348 KB correct
13 Correct 0 ms 348 KB correct
14 Correct 2 ms 348 KB correct
15 Correct 2 ms 456 KB correct
16 Correct 2 ms 348 KB correct
17 Correct 2 ms 348 KB correct
18 Correct 1 ms 344 KB correct
19 Correct 2 ms 348 KB correct
20 Correct 2 ms 348 KB correct
21 Correct 2 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 344 KB correct
30 Correct 1 ms 552 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 145 ms 2668 KB correct
35 Correct 149 ms 2612 KB correct
36 Correct 98 ms 2244 KB correct
37 Correct 4 ms 348 KB correct
38 Correct 140 ms 2664 KB correct
39 Correct 113 ms 2504 KB correct
40 Correct 94 ms 2264 KB correct
41 Correct 161 ms 2676 KB correct
42 Correct 153 ms 2884 KB correct
43 Correct 72 ms 1892 KB correct
44 Correct 57 ms 1544 KB correct
45 Correct 63 ms 1676 KB correct
46 Correct 48 ms 1452 KB correct
47 Correct 19 ms 856 KB correct
48 Correct 2 ms 344 KB correct
49 Correct 3 ms 604 KB correct
50 Correct 7 ms 1000 KB correct
51 Correct 47 ms 1668 KB correct
52 Correct 23 ms 1524 KB correct
53 Correct 26 ms 1460 KB correct
54 Correct 23 ms 1892 KB correct
55 Correct 55 ms 1676 KB correct
56 Correct 57 ms 1688 KB correct
57 Correct 14 ms 1684 KB correct
58 Correct 0 ms 348 KB correct
59 Correct 0 ms 348 KB correct
60 Incorrect 204 ms 7308 KB WA in grader: NO
61 Halted 0 ms 0 KB -