답안 #71602

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
71602 2018-08-25T08:22:06 Z 윤교준(#2220) Logistical Metropolis (KRIII5_LM) C++11
7 / 7
1678 ms 44060 KB
#include <bits/stdc++.h>
#define rf(x) (x)=0;while(*p<48)p++;while(47<*p)(x)=((x)<<3)+((x)<<1)+(*p++&15);
#define eb emplace_back
#define sz(V) ((int)(V).size())
#define allv(V) ((V).begin()),((V).end())
#define sorv(V) sort(allv(V))
#define revv(V) reverse(allv(V))
#define univ(V) (V).erase(unique(allv(V)),(V).end())
#define befv(V) ((V)[sz(V)-2])
#define upmax(a,b) (a)=max((a),(b))
#define upmin(a,b) (a)=min((a),(b))
#define INF (0x3f3f3f3f)
#define INFLL (0x3f3f3f3f3f3f3f3fll)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
static unsigned char str[22000022], *p=str;

const bool debug = 0;
const int MAXN = 100005;
const int MAXM = 300005;

struct EDG {
	int v, c, nxt;
} edg[MAXM*3];
int Bumsoo[MAXN], BumsooE[MAXN], edgn;

void addEdge(int a, int b, int c) {
	int idx = ++edgn;
	if(!Bumsoo[a]) {
		Bumsoo[a] = BumsooE[a] = idx;
		edg[idx].v = b;
		edg[idx].c = c;
		edg[idx].nxt = 0;
		return;
	}

	edg[BumsooE[a]].nxt = idx;
	edg[idx].v = b;
	edg[idx].c = c;
	edg[idx].nxt = 0;
	BumsooE[a] = idx;
}

vector<int> G[MAXN], T[MAXN];

int prt[17][MAXN], prtmxe[17][MAXN];
int dep[MAXN], cnt[MAXN], dfo[MAXN];

int A[MAXM], B[MAXM], C[MAXM], O[MAXM];
int ud[MAXN], isD[MAXN], isDn;
bitset<MAXM> isE;

ll MSTC;
int N, M;

int uf(int i) { return i == ud[i] ? i : (ud[i] = uf(ud[i])); }
void uf(int a, int b) { ud[uf(b)] = uf(a); }

int lca(int a, int b) {
	if(dep[a] > dep[b]) swap(a, b);
	const int dt = dep[b] - dep[a];
	for(int i = 0; i < 17; i++) if(dt & (1<<i)) b = prt[i][b];
	if(a == b) return a;
	for(int i = 17; i--;) if(prt[i][a] != prt[i][b]) {
		a = prt[i][a]; b = prt[i][b];
	}
	return prt[0][a];
}
int upmxe(int i, int l) {
	int ret = 0;
	for(int j = 0; j < 17; j++) if(l & (1<<j)) {
		upmax(ret, prtmxe[j][i]);
		i = prt[j][i];
	}
	return ret;
}

void f(int i, int &c) {
	cnt[i] = 1; dfo[i] = ++c;
	for(int e : T[i]) {
		int v = A[e] + B[e] - i;
		if(dep[v]) continue;
		prt[0][v] = i;
		prtmxe[0][v] = C[e];
		dep[v] = dep[i] + 1;
		f(v, c);
		cnt[i] += cnt[v];
	}
}

pll dfs(int idx) {
	if(!Bumsoo[idx]) return (isD[idx] == isDn) ? pll(-INFLL, 0) : pll(0, -INFLL);
	pll ret(0, 0); ll mx = -INFLL;
	if(isD[idx] == isDn) ret.first = -INFLL;

	for(int ei = Bumsoo[idx], v, c; ei; ei = edg[ei].nxt) {
		v = edg[ei].v; c = edg[ei].c;
		pll t = dfs(v);
		if(isD[idx] != isDn) {
			ll q = max(t.first, t.second + c);
			ret.first += q;
			ret.second += q;
			upmax(mx, -q + t.second);
		} else {
			ret.second += max(t.first, t.second + c);
		}
		if(ret.first < -INFLL) ret.first = -INFLL;
		if(ret.second < -INFLL) ret.second = -INFLL;
		if(INFLL < ret.first) ret.first = INFLL;
		if(INFLL < ret.second) ret.second = INFLL;
	}
	if(isD[idx] != isDn) ret.second += mx;
	upmax(ret.first, -INFLL);
	upmax(ret.second, -INFLL);
	upmin(ret.first, INFLL);
	upmin(ret.second, INFLL);
	return ret;
}

int main() {
	fread(str, 1, 22000022, stdin);

	rf(N) rf(M)
	for(int i = 1; i <= M; i++) {
		rf(A[i]) rf(B[i]) rf(C[i])
		G[A[i]].eb(i);
		G[B[i]].eb(i);
	}
	iota(O, O+M+1, 0); sort(O+1, O+M+1, [&](int a, int b) { return C[a] < C[b]; });
	iota(ud, ud+N+1, 0);
	for(int oi = 1, i; oi <= M; oi++) {
		i = O[oi];
		if(uf(A[i]) == uf(B[i])) continue;
		uf(A[i], B[i]);
		isE[i] = true;
		MSTC += C[i];
	}

	for(int i = 1; i <= M; i++) if(isE[i]) {
		T[A[i]].eb(i);
		T[B[i]].eb(i);
	}
	dep[1] = 1; { int c = 0; f(1, c); }
	for(int j = 1; j < 17; j++) for(int i = 1; i <= N; i++) {
		prt[j][i] = prt[j-1][prt[j-1][i]];
		prtmxe[j][i] = max(prtmxe[j-1][i], prtmxe[j-1][prt[j-1][i]]);
	}

	iota(ud, ud+N+1, 0);
	for(int i = 1; i <= N; i++) {
		vector<int> V;
		ll ret = MSTC; isDn++;
		isD[i] = isDn; V.eb(i);
		for(int e : G[i]) {
			int v = A[e] + B[e] - i;
			ret += C[e];
			isD[v] = isDn;
			V.eb(v);
		}
		sort(allv(V), [&](int a, int b) { return dfo[a] < dfo[b]; });
		vector<pii> PV;
		for(int i = 1, n = sz(V); i < n; i++) PV.eb(lca(V[i-1], V[i]), i);
		sort(allv(PV), [&](pii a, pii b) { return dfo[a.first] > dfo[b.first]; });

		for(auto &e : PV) {
			int p, a, b;
			tie(p, a) = e;
			b = V[a]; a = V[a-1];
			a = uf(a); b = uf(b);
			if(a != p) {
				addEdge(p, a, upmxe(a, dep[a]-dep[p]));
				uf(p, a);
			}
			if(b != p) {
				addEdge(p, b, upmxe(b, dep[b]-dep[p]));
				uf(p, b);
			}
		}

		if(!PV.empty()) ret -= dfs(PV.back().first).second;
		printf("%lld\n", ret);

		for(int v : V) {
			ud[v] = v;
			Bumsoo[v] = BumsooE[v] = 0;
		}
		for(auto &e : PV) {
			int v = e.first;
			ud[v] = v;
			Bumsoo[v] = BumsooE[v] = 0;
		}
		edgn = 0;
	}
	return 0;
}

Compilation message

LM.cpp: In function 'int main()':
LM.cpp:123:7: warning: ignoring return value of 'size_t fread(void*, size_t, size_t, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
  fread(str, 1, 22000022, stdin);
  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 5624 KB Output is correct
2 Correct 16 ms 5716 KB Output is correct
3 Correct 16 ms 5812 KB Output is correct
4 Correct 10 ms 5812 KB Output is correct
5 Correct 10 ms 5812 KB Output is correct
6 Correct 13 ms 5840 KB Output is correct
7 Correct 13 ms 5840 KB Output is correct
8 Correct 9 ms 5848 KB Output is correct
9 Correct 10 ms 5924 KB Output is correct
10 Correct 11 ms 5924 KB Output is correct
11 Correct 12 ms 5924 KB Output is correct
12 Correct 11 ms 5936 KB Output is correct
13 Correct 13 ms 5936 KB Output is correct
14 Correct 10 ms 5936 KB Output is correct
15 Correct 10 ms 6056 KB Output is correct
16 Correct 9 ms 6056 KB Output is correct
17 Correct 11 ms 6056 KB Output is correct
18 Correct 10 ms 6056 KB Output is correct
19 Correct 11 ms 6056 KB Output is correct
20 Correct 12 ms 6056 KB Output is correct
21 Correct 11 ms 6056 KB Output is correct
22 Correct 11 ms 6056 KB Output is correct
23 Correct 11 ms 6056 KB Output is correct
24 Correct 10 ms 6056 KB Output is correct
25 Correct 8 ms 6056 KB Output is correct
26 Correct 7 ms 6056 KB Output is correct
27 Correct 7 ms 6056 KB Output is correct
28 Correct 11 ms 6056 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1453 ms 42576 KB Output is correct
2 Correct 1640 ms 42576 KB Output is correct
3 Correct 1678 ms 42664 KB Output is correct
4 Correct 1464 ms 42664 KB Output is correct
5 Correct 1362 ms 42764 KB Output is correct
6 Correct 1232 ms 43032 KB Output is correct
7 Correct 1254 ms 43032 KB Output is correct
8 Correct 1195 ms 43032 KB Output is correct
9 Correct 1054 ms 44060 KB Output is correct
10 Correct 1079 ms 44060 KB Output is correct
11 Correct 829 ms 44060 KB Output is correct
12 Correct 982 ms 44060 KB Output is correct
13 Correct 1242 ms 44060 KB Output is correct
14 Correct 1128 ms 44060 KB Output is correct
15 Correct 900 ms 44060 KB Output is correct
16 Correct 1283 ms 44060 KB Output is correct
17 Correct 1349 ms 44060 KB Output is correct
18 Correct 1201 ms 44060 KB Output is correct
19 Correct 1198 ms 44060 KB Output is correct
20 Correct 1238 ms 44060 KB Output is correct
21 Correct 1282 ms 44060 KB Output is correct
22 Correct 1333 ms 44060 KB Output is correct
23 Correct 1593 ms 44060 KB Output is correct
24 Correct 1398 ms 44060 KB Output is correct
25 Correct 13 ms 5624 KB Output is correct
26 Correct 16 ms 5716 KB Output is correct
27 Correct 16 ms 5812 KB Output is correct
28 Correct 10 ms 5812 KB Output is correct
29 Correct 10 ms 5812 KB Output is correct
30 Correct 13 ms 5840 KB Output is correct
31 Correct 13 ms 5840 KB Output is correct
32 Correct 9 ms 5848 KB Output is correct
33 Correct 10 ms 5924 KB Output is correct
34 Correct 11 ms 5924 KB Output is correct
35 Correct 12 ms 5924 KB Output is correct
36 Correct 11 ms 5936 KB Output is correct
37 Correct 13 ms 5936 KB Output is correct
38 Correct 10 ms 5936 KB Output is correct
39 Correct 10 ms 6056 KB Output is correct
40 Correct 9 ms 6056 KB Output is correct
41 Correct 11 ms 6056 KB Output is correct
42 Correct 10 ms 6056 KB Output is correct
43 Correct 11 ms 6056 KB Output is correct
44 Correct 12 ms 6056 KB Output is correct
45 Correct 11 ms 6056 KB Output is correct
46 Correct 11 ms 6056 KB Output is correct
47 Correct 11 ms 6056 KB Output is correct
48 Correct 10 ms 6056 KB Output is correct
49 Correct 8 ms 6056 KB Output is correct
50 Correct 7 ms 6056 KB Output is correct
51 Correct 7 ms 6056 KB Output is correct
52 Correct 11 ms 6056 KB Output is correct