Submission #23189

# Submission time Handle Problem Language Result Execution time Memory
23189 2017-05-04T11:25:38 Z gs14004 Logistical Metropolis (KRIII5_LM) C++11
7 / 7
1506 ms 46056 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef pair<int, int> pi;

const int MAXN = 100005;
struct disj{
	int pa[MAXN], sz[MAXN];
	void init(int n){
		for(int i=0; i<=n; i++){
			pa[i] = i;
			sz[i] = 1;
		}
	}
	int find(int x){
		return pa[x] = (pa[x] == x ? x : find(pa[x]));
	}
	bool uni(int p, int q){
		p = find(p);
		q = find(q);
		if(p == q) return 0;
		pa[q] = p;
		sz[p] += sz[q];
		return 1;
	}
}disj;

struct edg{
	int s, e, x; 
	bool operator<(const edg &e)const{
		return x < e.x;
	}
}e[300005];

vector<pi> gph[100005];
vector<pi> edg[100005];

int n, m;
int par[17][100005], pae[17][100005], dfn[100005], dout[100005], piv;
int dep[100005];

bool in(int s, int t){
	return dfn[s] <= dfn[t] && dout[t] <= dout[s];
}

void dfs(int x, int p){
	dfn[x] = ++piv;
	for(auto &i : gph[x]){
		if(i.second != p){
			dep[i.second] = dep[x] + 1;
			par[0][i.second] = x;
			pae[0][i.second] = i.first;
			dfs(i.second, x);
		}
	}
	dout[x] = piv;
}

pi lca(int s, int e){
	if(dep[s] < dep[e]) swap(s, e);
	int dx = dep[s] - dep[e];
	int ret = 0;
	for(int i=0; i<17; i++){
		if((dx >> i) & 1){
			ret = max(ret, pae[i][s]);
			s = par[i][s];
		}
	}
	for(int i=16; i>=0; i--){
		if(par[i][s] != par[i][e]){
			ret = max({ret, pae[i][s], pae[i][e]});
			s = par[i][s];
			e = par[i][e];
		}
	}
	if(s != e){
		ret = max({ret, pae[0][s], pae[0][e]});
		s = par[0][s];
		e = par[0][e];
	}
	return pi(s, ret);
}

vector<pi> cnd[100005];
lint dp[100005][2];
int chk[100005];

void dfs2(int x, int p){
	lint up = -1e18;
	for(auto &i : cnd[x]){
		if(i.second == p) continue;
		dfs2(i.second, x);
		dp[i.second][0] = max(dp[i.second][0], dp[i.second][1] + i.first);
		dp[x][0] += dp[i.second][0];
		dp[x][1] += dp[i.second][0];
		up = max(up, dp[i.second][1] - dp[i.second][0]);
	}
	dp[x][1] += up;
	if(chk[x]){
		dp[x][1] = dp[x][0];
		dp[x][0] = -1e18;
	}
}

lint solve(int r){
	if(edg[r].empty()) return 0;
	vector<int> v = {r};
	for(auto &j : edg[r]){
		v.push_back(j.second);
	}
	sort(v.begin(), v.end(), [&](const int &a, const int &b){
		return dfn[a] < dfn[b];
	});
	int l = v.size();
	for(int i=1; i<l; i++) v.push_back(lca(v[i-1], v[i]).first);
	sort(v.begin(), v.end(), [&](const int &a, const int &b){
		return dfn[a] < dfn[b];
	});
	stack<int> stk;
	for(auto &i : v){
		if(!stk.empty() && stk.top() == i) continue;
		while(!stk.empty() && !in(stk.top(), i)) stk.pop();
		if(!stk.empty()){
			int l = lca(stk.top(), i).second;
			cnd[stk.top()].push_back(pi(l, i));
			cnd[i].push_back(pi(l, stk.top()));
		}
		stk.push(i);
	}
	lint ret = 0;
	for(auto &j : edg[r]){
		chk[j.second] = 1;
		ret += j.first;
	}
	dfs2(r, -1);
	ret -= dp[r][0];
	for(auto &i : v){
		cnd[i].clear();
		chk[i] = 0;
		dp[i][0] = dp[i][1] = 0;
	}
	return ret;
}

int main(){
	scanf("%d %d",&n,&m);
	for(int i=0; i<m; i++){
		scanf("%d %d %d",&e[i].s,&e[i].e,&e[i].x);
	}
	sort(e, e+m);
	disj.init(n);
	lint ans = 0;
	for(int i=0; i<m; i++){
		if(disj.uni(e[i].s, e[i].e)){
			gph[e[i].s].push_back(pi(e[i].x, e[i].e));
			gph[e[i].e].push_back(pi(e[i].x, e[i].s));
			ans += e[i].x;
		}
		edg[e[i].s].push_back(pi(e[i].x, e[i].e));
		edg[e[i].e].push_back(pi(e[i].x, e[i].s));
	}
	dfs(1, 0);
	for(int i=1; i<17; i++){
		for(int j=1; j<=n; j++){
			par[i][j] = par[i-1][par[i-1][j]];
			pae[i][j] = max(pae[i-1][j], pae[i-1][par[i-1][j]]);
		}
	}
	for(int i=1; i<=n; i++){
		printf("%lld\n", ans + solve(i));
	}
}

Compilation message

LM.cpp: In function 'int main()':
LM.cpp:146:22: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d",&n,&m);
                      ^
LM.cpp:148:44: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d %d",&e[i].s,&e[i].e,&e[i].x);
                                            ^
# Verdict Execution time Memory Grader output
1 Correct 9 ms 29900 KB Output is correct
2 Correct 3 ms 29900 KB Output is correct
3 Correct 6 ms 29900 KB Output is correct
4 Correct 6 ms 29900 KB Output is correct
5 Correct 6 ms 29900 KB Output is correct
6 Correct 6 ms 29900 KB Output is correct
7 Correct 9 ms 29900 KB Output is correct
8 Correct 9 ms 29900 KB Output is correct
9 Correct 6 ms 29900 KB Output is correct
10 Correct 9 ms 29900 KB Output is correct
11 Correct 6 ms 29900 KB Output is correct
12 Correct 3 ms 29900 KB Output is correct
13 Correct 3 ms 29900 KB Output is correct
14 Correct 9 ms 29900 KB Output is correct
15 Correct 3 ms 29900 KB Output is correct
16 Correct 6 ms 29900 KB Output is correct
17 Correct 6 ms 29900 KB Output is correct
18 Correct 3 ms 29900 KB Output is correct
19 Correct 6 ms 29900 KB Output is correct
20 Correct 6 ms 29900 KB Output is correct
21 Correct 6 ms 29900 KB Output is correct
22 Correct 9 ms 29900 KB Output is correct
23 Correct 6 ms 29900 KB Output is correct
24 Correct 6 ms 29900 KB Output is correct
25 Correct 3 ms 29768 KB Output is correct
26 Correct 3 ms 29768 KB Output is correct
27 Correct 0 ms 29768 KB Output is correct
28 Correct 3 ms 29900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1446 ms 45272 KB Output is correct
2 Correct 1429 ms 45276 KB Output is correct
3 Correct 1426 ms 45272 KB Output is correct
4 Correct 1473 ms 45272 KB Output is correct
5 Correct 1333 ms 45272 KB Output is correct
6 Correct 1166 ms 45320 KB Output is correct
7 Correct 1223 ms 45304 KB Output is correct
8 Correct 1226 ms 45508 KB Output is correct
9 Correct 1083 ms 46052 KB Output is correct
10 Correct 1123 ms 45696 KB Output is correct
11 Correct 849 ms 45984 KB Output is correct
12 Correct 909 ms 45568 KB Output is correct
13 Correct 1179 ms 45288 KB Output is correct
14 Correct 1216 ms 45292 KB Output is correct
15 Correct 956 ms 46056 KB Output is correct
16 Correct 1123 ms 43404 KB Output is correct
17 Correct 1193 ms 44816 KB Output is correct
18 Correct 1039 ms 44124 KB Output is correct
19 Correct 1179 ms 43444 KB Output is correct
20 Correct 1159 ms 44820 KB Output is correct
21 Correct 1373 ms 43384 KB Output is correct
22 Correct 1506 ms 43580 KB Output is correct
23 Correct 1146 ms 44276 KB Output is correct
24 Correct 1233 ms 43040 KB Output is correct
25 Correct 9 ms 29900 KB Output is correct
26 Correct 3 ms 29900 KB Output is correct
27 Correct 6 ms 29900 KB Output is correct
28 Correct 6 ms 29900 KB Output is correct
29 Correct 6 ms 29900 KB Output is correct
30 Correct 6 ms 29900 KB Output is correct
31 Correct 9 ms 29900 KB Output is correct
32 Correct 9 ms 29900 KB Output is correct
33 Correct 6 ms 29900 KB Output is correct
34 Correct 9 ms 29900 KB Output is correct
35 Correct 6 ms 29900 KB Output is correct
36 Correct 3 ms 29900 KB Output is correct
37 Correct 3 ms 29900 KB Output is correct
38 Correct 9 ms 29900 KB Output is correct
39 Correct 3 ms 29900 KB Output is correct
40 Correct 6 ms 29900 KB Output is correct
41 Correct 6 ms 29900 KB Output is correct
42 Correct 3 ms 29900 KB Output is correct
43 Correct 6 ms 29900 KB Output is correct
44 Correct 6 ms 29900 KB Output is correct
45 Correct 6 ms 29900 KB Output is correct
46 Correct 9 ms 29900 KB Output is correct
47 Correct 6 ms 29900 KB Output is correct
48 Correct 6 ms 29900 KB Output is correct
49 Correct 3 ms 29768 KB Output is correct
50 Correct 3 ms 29768 KB Output is correct
51 Correct 0 ms 29768 KB Output is correct
52 Correct 3 ms 29900 KB Output is correct