Submission #124866

#TimeUsernameProblemLanguageResultExecution timeMemory
124866DifferentHeavenFactories (JOI14_factories)C++17
0 / 100
8047 ms24312 KiB
#include <bits/stdc++.h>
// #include "factories.h"
#define ii pair <int, long long>
#define x first
#define y second
#define db(x) cerr << #x << " = " << x << endl;
#define _ << ", " << 

using namespace std;

inline void read(int &x){register int c = getchar();x = 0; int neg = 0;for (;((c<48 || c>57) && c != '-') ;c = getchar());if(c=='-') {neg=1;c=getchar();}for(;c>47 && c<58;c = getchar()) {x = (x<<1) + (x<<3) + c - 48;}if(neg) x=-x;}
inline void read(long long &x){register int c = getchar();x = 0; int neg = 0;for (;((c<48 || c>57) && c != '-') ;c = getchar());if(c=='-') {neg=1;c=getchar();}for(;c>47 && c<58;c = getchar()) {x = (x<<1) + (x<<3) + c - 48;}if(neg) x=-x;}
inline void writeln(long long x){char buffor[21];register int i=0;int neg=0; if (x<0) {neg=1; x= -x;}do{buffor[i++]=(x%10)+'0';x/=10;} while(x);i--;if (neg) putchar('-');while(i>=0) putchar(buffor[i--]);putchar('\n');}
inline void write(long long x){char buffor[21];register int i=0;int neg=0; if (x<0) {neg=1; x= -x;}do{buffor[i++]=(x%10)+'0';x/=10;} while(x);i--;if (neg) putchar('-');while(i>=0) putchar(buffor[i--]);putchar(' ');}

const int N = 5e5 + 7;
const long long oo = 1e18 + 7;

int n, q, nNode, Time;
long long dist;
int nChild[N], d[N], D[N];
bool blocked[N];
vector <ii> adj[N];
vector <ii> Adj[N];
int X[N], Y[N], t[N];
long long f[N];
ii par[N];

void DFS(int u){
	for (auto t: adj[u]){
		int v = t.x;
		int w = t.y;
		if (!d[v]){
			d[v] = d[u] + 1;
			D[v] = D[u] + w;
			DFS(v);
		}
	}
}

void dfs(int u, int p){
	nNode++;
	nChild[u] = 1;
	for (auto t: adj[u]){
		int v = t.x;
		if (v != p && !blocked[v]){
			dfs(v, u);
			nChild[u] += nChild[v];
		}
	}
}

int getCentroid(int u, int p){
	for (auto t: adj[u]){
		int v = t.x;
		int w = t.y;
		if (v != p && !blocked[v])
			if (nChild[v] > nNode / 2){
				dist += w;
				return getCentroid(v, u);
			}
	}	
	return u;
}

inline void addEdge(int u, int v, long long w){
	Adj[u].push_back(ii(v, w));
	Adj[v].push_back(ii(u, w));
}

void buildTree(int u, int p, int c, int dd){
	nNode = 0;
	dfs(u, u);

	dist = dd;
	int centroid = getCentroid(u, p);
	
	if (c != -1){
		addEdge(centroid, c, dist);
		par[centroid] = ii(c, dist);
	}
	else 
		par[centroid] = ii(-1, -1);

	blocked[centroid] = true;
	for (auto t: adj[centroid]){
		int v = t.x;
		int w = t.y;
		if (v != p && !blocked[v])
			buildTree(v, centroid, centroid, w);
	}
}

void Init(int pN, int pA[], int pB[], int pD[]){
	n = pN;
	for (int i = 0; i < n - 1; i++){
		int u = pA[i];
		int v = pB[i];
		int w = pD[i];
		adj[u].push_back(ii(v, w));
		adj[v].push_back(ii(u, w));
	}
}

inline void Update(int x){
	long long curDist = 0;
	while (1){
		if (t[x] != Time) {
			f[x] = oo;
			t[x] = Time;
		}
		f[x] = min(f[x], curDist);
		int pp = par[x].x;
		if (pp == -1) break;
		curDist += par[x].y;
		x = pp;
	}
}

long long Get(int x){
	long long curDist = 0;
	long long res = oo;
	while (1){
		if (t[x] != Time){
			f[x] = oo;
			t[x] = Time;
		}
		res = min(res, curDist + f[x]);
		int pp = par[x].x;
		if (pp == -1) break;
		curDist += par[x].y;
		x = pp;
	}
	return res;
}

long long Query(int S, int X[], int T, int Y[]){
	++Time;
	for (int i = 0; i < S; i++)
		Update(X[i]);

	long long res = oo;

	for (int i = 0; i < T; i++)
		res = min(res, Get(Y[i]));
	return res;
}

Compilation message (stderr)

factories.cpp: In function 'void read(int&)':
factories.cpp:11:39: warning: ISO C++1z does not allow 'register' storage class specifier [-Wregister]
 inline void read(int &x){register int c = getchar();x = 0; int neg = 0;for (;((c<48 || c>57) && c != '-') ;c = getchar());if(c=='-') {neg=1;c=getchar();}for(;c>47 && c<58;c = getchar()) {x = (x<<1) + (x<<3) + c - 48;}if(neg) x=-x;}
                                       ^
factories.cpp: In function 'void read(long long int&)':
factories.cpp:12:45: warning: ISO C++1z does not allow 'register' storage class specifier [-Wregister]
 inline void read(long long &x){register int c = getchar();x = 0; int neg = 0;for (;((c<48 || c>57) && c != '-') ;c = getchar());if(c=='-') {neg=1;c=getchar();}for(;c>47 && c<58;c = getchar()) {x = (x<<1) + (x<<3) + c - 48;}if(neg) x=-x;}
                                             ^
factories.cpp: In function 'void writeln(long long int)':
factories.cpp:13:63: warning: ISO C++1z does not allow 'register' storage class specifier [-Wregister]
 inline void writeln(long long x){char buffor[21];register int i=0;int neg=0; if (x<0) {neg=1; x= -x;}do{buffor[i++]=(x%10)+'0';x/=10;} while(x);i--;if (neg) putchar('-');while(i>=0) putchar(buffor[i--]);putchar('\n');}
                                                               ^
factories.cpp: In function 'void write(long long int)':
factories.cpp:14:61: warning: ISO C++1z does not allow 'register' storage class specifier [-Wregister]
 inline void write(long long x){char buffor[21];register int i=0;int neg=0; if (x<0) {neg=1; x= -x;}do{buffor[i++]=(x%10)+'0';x/=10;} while(x);i--;if (neg) putchar('-');while(i>=0) putchar(buffor[i--]);putchar(' ');}
                                                             ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...