답안 #62024

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
62024 2018-07-27T09:09:58 Z hamzqq9 Min-max tree (BOI18_minmaxtree) C++14
100 / 100
352 ms 21184 KB
#include<bits/stdc++.h>
#define st first
#define nd second
#define pb push_back
#define ppb pop_back
#define umax(x,y) x=max(x,y)
#define umin(x,y) x=min(x,y)
#define ll long long
#define ii pair<int,int>
#define iii pair<ii,int>
#define sz(x) (x.size())
#define orta ((bas+son)>>1)
#define all(x) x.begin(),x.end()
#define dbgs(x) cerr<<(#x)<<" --> "<<(x)<<" "
#define dbg(x) cerr<<(#x)<<" --> "<<(x)<<endl;getchar()
#define pw(x) (1<<(x))
#define inf 1000000005
#define MOD 1000000007
#define N 140015
#define LOG 20
using namespace std;

struct query {

	int from,to,cost;

	bool operator<(const query& oth) const {

		return cost<oth.cost;

	}

};

vector<query> Max,Min;
int n,q,x,y,z,totnode;
int dis[N],match[N],deep[N],ata[N],der[N],par[N];
vector<int> v[N],v2[N];

int bul(int node) {

	if(ata[node]==node) return node;

	return ata[node]=bul(ata[node]);

}

void dfs(int node) {

	if(node) {

		for(int i:v2[node]) {

			if(dis[node]+1==dis[match[i]]) {

				dfs(match[i]);

				if(dis[match[i]]!=inf) {

					match[node]=i;
					match[i]=node;

					return ;

				}

			}

		}

		dis[node]=inf;

	}

}

bool bfs() {

	queue<int> q;

	for(int i=1;i<=n;i++) {

		if(!match[i]) {

			dis[i]=0;
			q.push(i);

		}
		else dis[i]=inf;

	}

	dis[0]=inf;

	while(!q.empty()) {

		int node=q.front();

		q.pop();

		if(dis[node]<dis[0]) {

			for(int i:v2[node]) {

				if(dis[match[i]]==inf) {

					dis[match[i]]=dis[node]+1;

					q.push(match[i]);

				}

			}

		} 

	}

	return (dis[0]!=inf);

}

void hopcroft_karp() {

	while(bfs()) {

		for(int i=1;i<=n;i++) if(!match[i]) dfs(i);

	}

}

void make(int node,int from,int to) {

	while(1) {

		from=bul(from);
		to=bul(to);

		if(to==from) return ;

		if(der[from]<der[to]) swap(from,to);

		v2[node].pb(from);
		v2[from].pb(node);

		ata[from]=par[from];

	}

}

void init() {

	for(int i=1;i<=n;i++) {

		ata[i]=i;

	}

}

void dfs(int node,int ata) {

	par[node]=ata;
	der[node]=der[ata]+1;

	for(int i:v[node]) {

		if(i==ata) continue ;

		dfs(i,node);

	}

}

int main() {

//	freopen("input.txt","r",stdin);

	scanf("%d",&n);

	for(int i=1;i<n;i++) {

		scanf("%d %d",&x,&y);

		v[x].pb(y);
		v[y].pb(x);

	}	

	dfs(1,0);

	scanf("%d",&q);

	while(q--) {

		char c;

		scanf(" %c %d %d %d",&c,&x,&y,&z);

		if(c=='m') Min.pb({x,y,z});
		else Max.pb({x,y,z});

	}

	sort(all(Min));
	reverse(all(Min));
	sort(all(Max));

	init();

	for(int i=0;i<sz(Max);i++) make(i+1+n,Max[i].from,Max[i].to);

	init();

	for(int i=0;i<sz(Min);i++) make(i+1+sz(Max)+n,Min[i].from,Min[i].to);

	hopcroft_karp();

	for(int i=2;i<=n;i++) {

		int cst;

		if(!match[i]) {

			if(sz(v2[i])) {

				int node=v2[i][0];

				if(node<=n+sz(Max)) cst=Max[node-n-1].cost;
				else cst=Min[node-n-sz(Max)-1].cost;

			}
			else cst=1;

		}
		else if(match[i]<=n+sz(Max)) cst=Max[match[i]-n-1].cost;
		else cst=Min[match[i]-n-sz(Max)-1].cost;

		printf("%d %d %d\n",i,par[i],cst);

	}

}

Compilation message

minmaxtree.cpp: In function 'int main()':
minmaxtree.cpp:214:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<sz(Max);i++) make(i+1+n,Max[i].from,Max[i].to);
               ^
minmaxtree.cpp:218:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<sz(Min);i++) make(i+1+sz(Max)+n,Min[i].from,Min[i].to);
               ^
minmaxtree.cpp:232:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     if(node<=n+sz(Max)) cst=Max[node-n-1].cost;
            ^
minmaxtree.cpp:239:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   else if(match[i]<=n+sz(Max)) cst=Max[match[i]-n-1].cost;
                   ^
minmaxtree.cpp:182:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d",&n);
  ~~~~~^~~~~~~~~
minmaxtree.cpp:186:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d",&x,&y);
   ~~~~~^~~~~~~~~~~~~~~
minmaxtree.cpp:195:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d",&q);
  ~~~~~^~~~~~~~~
minmaxtree.cpp:201:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf(" %c %d %d %d",&c,&x,&y,&z);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 6904 KB Output is correct
2 Correct 14 ms 7012 KB Output is correct
3 Correct 11 ms 7012 KB Output is correct
4 Correct 11 ms 7152 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 279 ms 19788 KB Output is correct
2 Correct 231 ms 19788 KB Output is correct
3 Correct 221 ms 19788 KB Output is correct
4 Correct 270 ms 20400 KB Output is correct
5 Correct 224 ms 20400 KB Output is correct
6 Correct 217 ms 20400 KB Output is correct
7 Correct 200 ms 20400 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 194 ms 20400 KB Output is correct
2 Correct 175 ms 20400 KB Output is correct
3 Correct 200 ms 20400 KB Output is correct
4 Correct 182 ms 20400 KB Output is correct
5 Correct 219 ms 20400 KB Output is correct
6 Correct 215 ms 20400 KB Output is correct
7 Correct 189 ms 20400 KB Output is correct
8 Correct 241 ms 20400 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 6904 KB Output is correct
2 Correct 14 ms 7012 KB Output is correct
3 Correct 11 ms 7012 KB Output is correct
4 Correct 11 ms 7152 KB Output is correct
5 Correct 279 ms 19788 KB Output is correct
6 Correct 231 ms 19788 KB Output is correct
7 Correct 221 ms 19788 KB Output is correct
8 Correct 270 ms 20400 KB Output is correct
9 Correct 224 ms 20400 KB Output is correct
10 Correct 217 ms 20400 KB Output is correct
11 Correct 200 ms 20400 KB Output is correct
12 Correct 194 ms 20400 KB Output is correct
13 Correct 175 ms 20400 KB Output is correct
14 Correct 200 ms 20400 KB Output is correct
15 Correct 182 ms 20400 KB Output is correct
16 Correct 219 ms 20400 KB Output is correct
17 Correct 215 ms 20400 KB Output is correct
18 Correct 189 ms 20400 KB Output is correct
19 Correct 241 ms 20400 KB Output is correct
20 Correct 260 ms 20400 KB Output is correct
21 Correct 252 ms 20400 KB Output is correct
22 Correct 256 ms 20400 KB Output is correct
23 Correct 242 ms 20400 KB Output is correct
24 Correct 280 ms 20400 KB Output is correct
25 Correct 284 ms 21184 KB Output is correct
26 Correct 352 ms 21184 KB Output is correct
27 Correct 302 ms 21184 KB Output is correct
28 Correct 321 ms 21184 KB Output is correct
29 Correct 229 ms 21184 KB Output is correct
30 Correct 255 ms 21184 KB Output is correct
31 Correct 240 ms 21184 KB Output is correct
32 Correct 253 ms 21184 KB Output is correct
33 Correct 266 ms 21184 KB Output is correct
34 Correct 236 ms 21184 KB Output is correct
35 Correct 232 ms 21184 KB Output is correct