Submission #40961

# Submission time Handle Problem Language Result Execution time Memory
40961 2018-02-10T14:49:48 Z Kerim Sightseeing (NOI14_sightseeing) C++14
15 / 25
3500 ms 249572 KB
#include "bits/stdc++.h"
#define MAXN 500009
#define INF 1000000007
#define mp(x,y) make_pair(x,y)
#define all(v) v.begin(),v.end()
#define pb(x) push_back(x)
#define wr cout<<"----------------"<<endl;
#define ppb() pop_back()
#define tr(ii,c) for(__typeof((c).begin()) ii=(c).begin();ii!=(c).end();ii++)
#define ff first
#define ss second
#define my_little_dodge 46
#define debug(x)  cerr<< #x <<" = "<< x<<endl;
using namespace std;

typedef long long ll;
typedef pair<int,int> PII;
template<class T>bool umin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T>bool umax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
int ata[MAXN],ans[MAXN];
vector<int>s[MAXN];
vector<PII>adj[MAXN];
int tap(int x){
	if(ata[x]==x)
		return x;
	return ata[x]=tap(ata[x]);		
}
void merge(int x,int y,int val){
	if((x=tap(x))==(y=tap(y)))
		return;
	int root=tap(1);
	if(x==root){
		tr(it,s[y]){
			ans[*it]=val;
			s[x].pb(*it);
		}
		s[y].clear();
		ata[y]=x;
	}
	else if(y==root){
		tr(it,s[x]){
			ans[*it]=val;
			s[y].pb(*it);
		}
		s[x].clear();
		ata[x]=y;
	}
	else{
		if(s[x].size()<s[y].size())
			swap(x,y);
		tr(it,s[y])
			s[x].pb(*it);
		s[y].clear();
		ata[y]=x;	
	}	
}
int main(){
    //~ freopen("file.in", "r", stdin);
    int n,m,q;
    scanf("%d%d%d",&n,&m,&q);
    for(int i=1;i<=n;i++)	
		ata[i]=i,s[i].pb(i);
	for(int i=1;i<=m;i++){
		int u,v,w;
		scanf("%d%d%d",&u,&v,&w);
		adj[w].pb(mp(u,v));
	}
	for(int i=MAXN-1;i>=0;i--){
		tr(it,adj[i])
			merge(it->ff,it->ss,i);
	}
	while(q--){
		int x;
		scanf("%d",&x);
		printf("%d\n",ans[x]);
	}
	return 0;
}

Compilation message

sightseeing.cpp: In function 'int main()':
sightseeing.cpp:60:29: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d%d",&n,&m,&q);
                             ^
sightseeing.cpp:65:27: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d%d",&u,&v,&w);
                           ^
sightseeing.cpp:74:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d",&x);
                 ^
# Verdict Execution time Memory Grader output
1 Correct 23 ms 23800 KB Output is correct
2 Correct 22 ms 23800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 24076 KB Output is correct
2 Correct 24 ms 24076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 66 ms 28100 KB Output is correct
2 Correct 58 ms 28556 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2411 ms 143064 KB Output is correct
2 Execution timed out 3588 ms 249572 KB Time limit exceeded