답안 #62017

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
62017 2018-07-27T08:55:50 Z hamzqq9 Min-max tree (BOI18_minmaxtree) C++14
29 / 100
291 ms 25004 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 240015
#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]) cst=10;
		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:227: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 18 ms 11640 KB Output is correct
2 Correct 18 ms 11752 KB Output is correct
3 Correct 18 ms 11752 KB Output is correct
4 Correct 14 ms 11752 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 291 ms 24504 KB Output is correct
2 Correct 187 ms 24504 KB Output is correct
3 Correct 283 ms 24504 KB Output is correct
4 Correct 215 ms 25004 KB Output is correct
5 Correct 195 ms 25004 KB Output is correct
6 Correct 217 ms 25004 KB Output is correct
7 Correct 200 ms 25004 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 183 ms 25004 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 11640 KB Output is correct
2 Correct 18 ms 11752 KB Output is correct
3 Correct 18 ms 11752 KB Output is correct
4 Correct 14 ms 11752 KB Output is correct
5 Correct 291 ms 24504 KB Output is correct
6 Correct 187 ms 24504 KB Output is correct
7 Correct 283 ms 24504 KB Output is correct
8 Correct 215 ms 25004 KB Output is correct
9 Correct 195 ms 25004 KB Output is correct
10 Correct 217 ms 25004 KB Output is correct
11 Correct 200 ms 25004 KB Output is correct
12 Incorrect 183 ms 25004 KB Output isn't correct
13 Halted 0 ms 0 KB -