Submission #61598

# Submission time Handle Problem Language Result Execution time Memory
61598 2018-07-26T07:54:36 Z khsoo01 Min-max tree (BOI18_minmaxtree) C++11
100 / 100
283 ms 16060 KB
#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;

const int N = 70005;

int n, m, dep[N], par[N], ans[N];
bool vv[N], ve[N];

vector<int> tr[N], cyc, usd;
vector<pii> adj[N];

struct edge {
	int a, b, x;
	bool operator < (edge &T) {
		return x < T.x;
	}
} e[N];

struct coloring {
	int c[N], p[N];
	int Find (int X) {
		if(p[X] == X) return X;
		return p[X] = Find(p[X]);
	}
	void init () {
		for(int i=1;i<=n;i++) {
			c[i] = 0;
			p[i] = i;
		}
	}
	void color (int A, int B, int C) {
		while(true) {
			A = Find(A);
			B = Find(B);
			if(A == B) return;
			if(dep[A] < dep[B]) swap(A, B);
			c[A] = C;
			p[A] = par[A];
		}
	}
} col[2];

void calc (int C, int P) {
	dep[C] = dep[P] + 1;
	par[C] = P;
	for(auto &T : tr[C]) {
		if(T == P) continue;
		calc(T, C);
	}
}

bool findcyc (int I) {
	if(vv[I]) return true;
	vv[I] = true;
	for(auto &T : adj[I]) {
		int A, B;
		tie(A, B) = T;
		if(ve[B]) continue;
		ve[B] = true;
		if(findcyc(A)) {
			cyc.push_back(I);
			ans[B] = I;
			return true;
		}
	}
	usd.push_back(I);
	return false;
}

void dfs (int I) {
	vv[I] = true;
	for(auto &T : adj[I]) {
		int A, B;
		tie(A, B) = T;
		if(vv[A]) continue;
		ans[B] = A;
		dfs(A);
	}
}

int main()
{
	scanf("%d",&n);
	for(int i=1;i<n;i++) {
		int A, B;
		scanf("%d%d",&A,&B);
		tr[A].push_back(B);
		tr[B].push_back(A);
	}
	calc(1, 0);
	scanf("%d",&m);
	for(int i=1;i<=m;i++) {
		char typ[2];
		scanf("%s%d%d%d",typ,&e[i].a,&e[i].b,&e[i].x);
		if(typ[0] == 'm') e[i].x *= -1;
	}
	sort(e+1, e+1+m);
	col[0].init();
	col[1].init();
	for(int i=1;i<=m;i++) {
		col[e[i].x > 0].color(e[i].a, e[i].b, i);
	}
	for(int i=1;i<=n;i++) {
		int A = col[0].c[i], B = col[1].c[i];
		adj[A].push_back({B, i});
		adj[B].push_back({A, i});
	}
	for(int i=0;i<=m;i++) {
		if(vv[i]) continue;
		findcyc(i);
		for(auto &T : usd) {
			vv[T] = false;
		}
		usd.clear();
		for(auto &T : cyc) {
			dfs(T);
		}
		cyc.clear();
	}
	for(int i=2;i<=n;i++) {
		int T = 1;
		if(ans[i]) T = ans[i];
		else if(col[0].c[i]) T = col[0].c[i];
		else if(col[1].c[i]) T = col[1].c[i];
		printf("%d %d %d\n", par[i], i, abs(e[T].x));
	}
}

Compilation message

minmaxtree.cpp: In function 'int main()':
minmaxtree.cpp:84:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d",&n);
  ~~~~~^~~~~~~~~
minmaxtree.cpp:87:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d",&A,&B);
   ~~~~~^~~~~~~~~~~~~~
minmaxtree.cpp:92:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d",&m);
  ~~~~~^~~~~~~~~
minmaxtree.cpp:95:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%s%d%d%d",typ,&e[i].a,&e[i].b,&e[i].x);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 3704 KB Output is correct
2 Correct 5 ms 3800 KB Output is correct
3 Correct 5 ms 3848 KB Output is correct
4 Correct 6 ms 3864 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 160 ms 14832 KB Output is correct
2 Correct 176 ms 14832 KB Output is correct
3 Correct 176 ms 14832 KB Output is correct
4 Correct 162 ms 15544 KB Output is correct
5 Correct 163 ms 15544 KB Output is correct
6 Correct 223 ms 15544 KB Output is correct
7 Correct 178 ms 15544 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 123 ms 15544 KB Output is correct
2 Correct 145 ms 15544 KB Output is correct
3 Correct 122 ms 15544 KB Output is correct
4 Correct 117 ms 15544 KB Output is correct
5 Correct 140 ms 15544 KB Output is correct
6 Correct 173 ms 15544 KB Output is correct
7 Correct 124 ms 15544 KB Output is correct
8 Correct 179 ms 15544 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 3704 KB Output is correct
2 Correct 5 ms 3800 KB Output is correct
3 Correct 5 ms 3848 KB Output is correct
4 Correct 6 ms 3864 KB Output is correct
5 Correct 160 ms 14832 KB Output is correct
6 Correct 176 ms 14832 KB Output is correct
7 Correct 176 ms 14832 KB Output is correct
8 Correct 162 ms 15544 KB Output is correct
9 Correct 163 ms 15544 KB Output is correct
10 Correct 223 ms 15544 KB Output is correct
11 Correct 178 ms 15544 KB Output is correct
12 Correct 123 ms 15544 KB Output is correct
13 Correct 145 ms 15544 KB Output is correct
14 Correct 122 ms 15544 KB Output is correct
15 Correct 117 ms 15544 KB Output is correct
16 Correct 140 ms 15544 KB Output is correct
17 Correct 173 ms 15544 KB Output is correct
18 Correct 124 ms 15544 KB Output is correct
19 Correct 179 ms 15544 KB Output is correct
20 Correct 283 ms 15544 KB Output is correct
21 Correct 164 ms 15544 KB Output is correct
22 Correct 132 ms 15544 KB Output is correct
23 Correct 160 ms 15544 KB Output is correct
24 Correct 234 ms 15544 KB Output is correct
25 Correct 181 ms 16060 KB Output is correct
26 Correct 173 ms 16060 KB Output is correct
27 Correct 201 ms 16060 KB Output is correct
28 Correct 205 ms 16060 KB Output is correct
29 Correct 226 ms 16060 KB Output is correct
30 Correct 179 ms 16060 KB Output is correct
31 Correct 245 ms 16060 KB Output is correct
32 Correct 167 ms 16060 KB Output is correct
33 Correct 205 ms 16060 KB Output is correct
34 Correct 164 ms 16060 KB Output is correct
35 Correct 214 ms 16060 KB Output is correct