Submission #482045

# Submission time Handle Problem Language Result Execution time Memory
482045 2021-10-22T18:01:04 Z PoPularPlusPlus Rigged Roads (NOI19_riggedroads) C++17
19 / 100
342 ms 80480 KB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long 
#define pb(e) push_back(e)
#define sv(a) sort(a.begin(),a.end())
#define sa(a,n) sort(a,a+n)
#define mp(a,b) make_pair(a,b)
#define vf first
#define vs second
#define ar array
#define all(x) x.begin(),x.end()
const int inf = 0x3f3f3f3f;
const int mod = 1000000007; 
const double PI=3.14159265358979323846264338327950288419716939937510582097494459230;
bool remender(ll a , ll b){return a%b;}

const int N = 300003;

struct UFDS {

int p[N],siz[N];

void init(int n){
	for(int i = 0; i <= n; i++){
		p[i] = i;
		siz[i] = 1;
	}
}

int get(int x){
	return p[x]=(x==p[x] ? x : get(p[x]));
}

void unio(int u , int v){
	int a = get(u);
	int b=get(v);
	if(a==b)return;
	//if(siz[a]>siz[b])swap(a,b);
	p[b]=a;
	siz[a]+=siz[b];
}
};

const int l = 25;
vector<pair<int,int>> adj[N];
vector<pair<int,int>> vp(N);
int idx[N];
int timer;
int tin[N], tout[N];
int up[N][l + 5];
int ans[N];
int cur;
int there[N];
UFDS d;

void dfs(int v, int p){
    tin[v] = ++timer;
    up[v][0] = p;
    for (int i = 1; i <= l; ++i)
        up[v][i] = up[up[v][i-1]][i-1];

    for (auto &[u , id] : adj[v]) {
        if (u != p){
            dfs(u, v);
            idx[u] = id;
		}
    }

    tout[v] = ++timer;
}

bool is_ancestor(int u, int v){
    return tin[u] <= tin[v] && tout[u] >= tout[v];
}

int lca(int u, int v){
    if (is_ancestor(u, v))
        return u;
    if (is_ancestor(v, u))
        return v;
    for (int i = l; i >= 0; --i) {
        if (!is_ancestor(up[u][i], v))
            u = up[u][i];
    }
    return up[u][0];
}

void help(int x , int y){
	int lc = lca(x,y);
	int u = d.get(x) , v = d.get(y);
	vector<int> ed;
	while(u != lc && is_ancestor(lc , u) == lc){
		ed.pb(idx[u]);
		d.unio(d.get(up[u][0]) , u);
		u = d.get(u);
	}
	while(v != lc && is_ancestor(lc , v) == lc){
		ed.pb(idx[v]);
		d.unio(d.get(up[v][0]) , v);
		v = d.get(v);
	}
	sv(ed);
	for(int i : ed){
		if(ans[i] == 0)ans[i] = cur++;
	}
}
 
void solve(){
	int n , m;
	cin >> n >> m;
	for(int i = 0; i < m; i++){
		int a,b;
		cin >> a >> b;
		vp[i] = mp(a,b);
	}
	d.init(n + 2);
	memset(there , 0, sizeof there);
	for(int i = 1; i < n; i++){
		int j;
		cin >> j;
		j--;
		there[j] = 1;
		adj[vp[j].vf].pb(mp(vp[j].vs , j));
		adj[vp[j].vs].pb(mp(vp[j].vf , j));
	}
	timer = 0;
	dfs(1,1);
	memset(ans,0,sizeof ans);
	cur = 1;
	for(int i = 0; i < m; i++){
		if(ans[i] != 0)continue;
		if(there[i] == 1){
			ans[i] = cur++;
		}
		else {
			help(vp[i].vf , vp[i].vs);
			ans[i] = cur++;
		}
	}
	for(int i = 0; i < m; i++){
		cout << ans[i] << ' ';
	}
	cout << '\n';
}
 
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
	//int t;cin >> t;while(t--)
	solve();
	return 0;
}
# Verdict Execution time Memory Grader output
1 Incorrect 7 ms 11980 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 7 ms 11980 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 67 ms 31304 KB Output is correct
2 Correct 122 ms 38228 KB Output is correct
3 Correct 115 ms 20416 KB Output is correct
4 Correct 153 ms 65620 KB Output is correct
5 Correct 181 ms 68140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 119 ms 39364 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 314 ms 67876 KB Output is correct
2 Correct 285 ms 75244 KB Output is correct
3 Correct 63 ms 29904 KB Output is correct
4 Correct 111 ms 38984 KB Output is correct
5 Correct 342 ms 80480 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 263 ms 50064 KB Output is correct
2 Incorrect 136 ms 38340 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 7 ms 11980 KB Output isn't correct
2 Halted 0 ms 0 KB -