Submission #376089

# Submission time Handle Problem Language Result Execution time Memory
376089 2021-03-10T21:09:07 Z SavicS Sightseeing (NOI14_sightseeing) C++14
15 / 25
3500 ms 200736 KB
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>

#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define ff(i,a,b) for(int i=a;i<=b;i++)
#define fb(i,b,a) for(int i=b;i>=a;i--)

using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn = 500005;
const int inf = 1e9 + 5;

template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

// os.order_of_key(k) the number of elements in the os less than k
// *os.find_by_order(k)  print the k-th smallest number in os(0-based)

int n, m, q;

vector<pii> g[maxn];

int par[maxn], sz[maxn];
int findpar(int x){
	if(x == par[x])return x;
	return par[x] = findpar(par[x]);
}
void unite(int x, int y){
	int a = findpar(x);
	int b = findpar(y);
	if(a == b)return;
	if(sz[a] > sz[b])swap(a, b);
	par[b] = a;
	sz[a] += sz[b]; 
}
void init(){
	ff(i,1,n){
		par[i] = i;
		sz[i] = 1;
	}
}

vector<array<int,3>> grana;
bool cmp(array<int,3> s1, array<int,3> s2){
	return s1[2] > s2[2];
}
void kruskal(){
	init();
	sort(all(grana), cmp);
	for(auto c : grana){
		int u = c[0];
		int v = c[1];
		int w = c[2];
		if(findpar(u) != findpar(v)){
			unite(u, v);
			g[u].pb({v, w});
			g[v].pb({u, w});
		}
	}
}

int kol[maxn];
void dfs(int v, int p){
	for(auto c : g[v]){
		int u = c.fi;
		int w = c.se;
		if(u != p){
			kol[u] = min(kol[v], w);
			dfs(u, v);
		}
	}
	if(v == 1)kol[v] = 0;
}

int main()
{
   	ios::sync_with_stdio(false);
   	cout.tie(nullptr);
  	cin.tie(nullptr);
	cin >> n >> m >> q;
	ff(i,1,m){
		int u, v, w;
		cin >> u >> v >> w;
		grana.pb({u, v, w});
	}
	kruskal();
	kol[1] = inf;
	dfs(1, -1);
	while(q--){
		int x;
		cin >> x;
		cout << kol[x] << endl;
	}
   	return 0;
}
/**

4 4 2
1 2 10
1 3 30
2 4 20
3 4 5

// probati bojenje sahovski ili slicno

**/


# Verdict Execution time Memory Grader output
1 Correct 9 ms 12140 KB Output is correct
2 Correct 9 ms 12140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 12268 KB Output is correct
2 Correct 9 ms 12140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 57 ms 15352 KB Output is correct
2 Correct 50 ms 14948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2487 ms 125248 KB Output is correct
2 Execution timed out 3580 ms 200736 KB Time limit exceeded