Submission #82627

# Submission time Handle Problem Language Result Execution time Memory
82627 2018-11-01T06:06:36 Z Good Min-max tree (BOI18_minmaxtree) C++11
29 / 100
319 ms 38652 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
 
#define ff first
#define ss second
#define Maxn 70009
#define ll long long
#define pb push_back
#define Inf 1000000009
#define ppb() pop_back()
#define pii pair <int , int>
#define mid(x, y) (x + y) / 2
#define all(x) x.begin(),x.end()
#define llInf 1000000000000000009
#define tr(i, c) for(__typeof(c).begin() i = (c).begin() ; i != (c).end() ; i++)
using namespace std;
using namespace __gnu_pbds;
typedef tree <int, null_type, less <int>, rb_tree_tag, tree_order_statistics_node_update> order;

char c[Maxn]; 
bool vis[Maxn];
 
int n, q;
int T[Maxn];
int lvl[Maxn];
int mat[Maxn][20];
int l[Maxn], r[Maxn];
int E[Maxn][2], P[Maxn][2];
int x[Maxn], y[Maxn], z[Maxn];
 
vector <int> M[Maxn];
vector <int> adj[Maxn];
vector <pair <int, pair <int, pii> > > A[2];
 
map <int, int> mp;
 
void dfs (int nd, int p, int d) {
	mat[nd][0] = p;
	lvl[nd] = lvl[p] + 1;
	
	if (d == 1 and nd != 1) {
		if (E[nd][0])
			M[nd].pb (E[nd][0]);
				
		if (E[nd][1])
			M[nd].pb (E[nd][1]);
	}
	
	if (d == 2 and nd != 1) {
		if (!l[nd])
			printf ("%d %d %d\n", p, nd, T[E[nd][0]]);
		else
			printf ("%d %d %d\n", p, nd, T[l[nd]]);	
	}
	
	for (auto i: adj[nd])
		if (i != p)		
			dfs (i, nd, d);
}
 
bool dfs1 (int nd) {
	if (vis[nd])
		return 0;
	
	vis[nd] = 1;	
	for (auto i: M[nd])
		if (!r[i] or dfs1 (r[i])) {
			l[nd] = i;
			r[i] = nd;
			return 1;
		}
	
	return 0;	
} 
 
void build () {
	for (int i = 1; i <= 18; i++)
		for (int j = 1; j <= n; j++)
			if (mat[j][i - 1] != -1)
				mat[j][i] = mat[mat[j][i - 1]][i - 1];	
}
 
int LCA (int a, int b) {
	if (lvl[a] < lvl[b])
		swap (a, b);
		
	for (int i = 18; i >= 0; i--)
		if (lvl[mat[a][i]] >= lvl[b])
			a = mat[a][i];
		
	if (a == b)
		return a;
	
	for (int i = 18; i >= 0; i--)
		if (mat[a][i] != -1 and mat[a][i] != mat[b][i])
			a = mat[a][i], b = mat[b][i];
		
	return mat[a][0];					
}
 
int dsu (int x, bool d) {
	if (P[x][d] == x)
		return x;	
	
	return P[x][d] = dsu (P[x][d], d);	
}
 
void solve (int x, int y, int z, bool d) {
	x = dsu (P[x][d], d);
			
	if (lvl[x] <= lvl[y])
		return;		
	
	E[x][d] = z;
	
	if (mat[x][0] != -1)
		P[x][d] = dsu (mat[x][0], d);
	
	x = P[x][d];
	
	solve (x, y, z, d);	
}
 
int main () {
	//freopen ("file.in", "r", stdin);
	//freopen ("file.out", "w", stdout);
	
 	//srand ((unsigned) time ( NULL ));
	//int randomNumber = rand() % 10 + 1;
 
	scanf ("%d", &n);
	for (int i = 1; i < n; i++) {
		int u, v;
		scanf ("%d%d", &u, &v);
		
		adj[u].pb (v);
		adj[v].pb (u);
		P[i][0] = P[i][1] = i;
	}
	
	P[n][0] = P[n][1] = n;
	
	memset (mat, -1, sizeof (mat));
	dfs (1, -1, 0);
	build ();
	
	scanf ("%d", &q);
	
	for (int i = 1; i <= q; i++) {		
		scanf (" %c%d%d%d", &c[i], &x[i], &y[i], &z[i]);
		mp[z[i]] = 1;	
	}
	
	int cnt = 0;
	for (auto i: mp)
		mp[i.ff] = ++cnt;
	
	for (int i = 1; i <= q; i++) {
		int l = LCA (x[i], y[i]);
		
		if (c[i] == 'M')
			A[0].pb ({mp[z[i]], {l, {x[i], y[i]}}});
		else
			A[1].pb ({mp[z[i]], {l, {x[i], y[i]}}});		
		
		T[mp[z[i]]] = z[i];
	}
	
	sort (all (A[0]));
	sort (all (A[1]));
	reverse (all (A[1]));
	
	for (auto i: A[0]) {
		solve (i.ss.ss.ff, i.ss.ff, i.ff, 0);
		solve (i.ss.ss.ss, i.ss.ff, i.ff, 0);	
	}
		
	for (auto i: A[1]) {
		solve (i.ss.ss.ff, i.ss.ff, i.ff, 1);
		solve (i.ss.ss.ss, i.ss.ff, i.ff, 1);
	}
	
	dfs (1, -1, 1);
	
	bool ok = 1;
	while (ok) {
		ok = 0;
		memset (vis, 0, sizeof (vis));
		
		for (int i = 2; i <= n; i++)
			if (!l[i] and dfs1 (i))
				ok = 1;
	}

	dfs (1, -1, 2);	
	return 0;
}

Compilation message

minmaxtree.cpp: In function 'int main()':
minmaxtree.cpp:131:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf ("%d", &n);
  ~~~~~~^~~~~~~~~~
minmaxtree.cpp:134:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf ("%d%d", &u, &v);
   ~~~~~~^~~~~~~~~~~~~~~~
minmaxtree.cpp:147:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf ("%d", &q);
  ~~~~~~^~~~~~~~~~
minmaxtree.cpp:150:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf (" %c%d%d%d", &c[i], &x[i], &y[i], &z[i]);
   ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 10 ms 9208 KB Output is correct
2 Correct 10 ms 9220 KB Output is correct
3 Correct 9 ms 9264 KB Output is correct
4 Correct 12 ms 9440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 319 ms 26896 KB Output is correct
2 Correct 291 ms 27324 KB Output is correct
3 Correct 306 ms 29648 KB Output is correct
4 Correct 314 ms 34348 KB Output is correct
5 Correct 289 ms 34348 KB Output is correct
6 Correct 265 ms 36768 KB Output is correct
7 Correct 282 ms 38652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 160 ms 38652 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 10 ms 9208 KB Output is correct
2 Correct 10 ms 9220 KB Output is correct
3 Correct 9 ms 9264 KB Output is correct
4 Correct 12 ms 9440 KB Output is correct
5 Correct 319 ms 26896 KB Output is correct
6 Correct 291 ms 27324 KB Output is correct
7 Correct 306 ms 29648 KB Output is correct
8 Correct 314 ms 34348 KB Output is correct
9 Correct 289 ms 34348 KB Output is correct
10 Correct 265 ms 36768 KB Output is correct
11 Correct 282 ms 38652 KB Output is correct
12 Incorrect 160 ms 38652 KB Output isn't correct
13 Halted 0 ms 0 KB -