Submission #441695

# Submission time Handle Problem Language Result Execution time Memory
441695 2021-07-05T20:23:39 Z AriaH Toll (APIO13_toll) C++11
78 / 100
2500 ms 30432 KB
/** work as hard as you can and keep the first place **/

#pragma GCC optimize("Ofast")
#pragma GCC target("avx")
#include<bits/stdc++.h>

using namespace std;

typedef long long			ll;
typedef long double			ld;
typedef pair < int, int > 	pii;
typedef pair < ll, ll > 	pll;

#define F					first
#define S					second
#define all(x)				x.begin(), x.end()
#define SZ(x)				(int)x.size()
#define Mp					make_pair

const int N = 3e5 + 50;
const ll mod = 1e9 + 7;
const ll inf = 8e18;
const int LOG = 20;

ll tot, ret, sub[N], Size[N];

int n, m, k, ptr, ROOT, sz[N], V[N], U[N], C[N], par[N], ord[N], in1[N], in2[N], T[N], query[N], St[N], Fi[N];

vector < int > vec, roots, G[N];

inline void init() { for(int i = 1; i <= n; i ++) sz[i] = 1, par[i] = i; }

int get(int x) { return (x == par[x]? x : par[x] = get(par[x])); }

bool unify(int v, int u)
{
	v = get(v), u = get(u);
	if(v == u) return 0;
	if(sz[u] > sz[v]) swap(u, v);
	par[u] = v;
	return 1;
}

bool cmp(int i, int j)
{
	return C[i] < C[j];
}

set < pii > st[N];

void pre(int v, int last = 0)
{
	St[v] = ++ptr;
	for(auto id : G[v])
	{
		if(id == last) continue;
		int u = V[id] ^ U[id] ^ v;
		pre(u, id);
	}
	Fi[v] = ptr;
	///printf("v = %d St = %d Fi = %d\n", v, St[v], Fi[v]);
}

void dfs(int v, int last = 0)
{
	sub[v] = Size[v];
	for(auto id : G[v])
	{
		if(id == last) continue;
		int u = V[id] ^ U[id] ^ v;
		dfs(u, id);
		if(SZ(st[v]) < SZ(st[u])) st[v].swap(st[u]);
		for(auto now : st[u])
		{
			int node = now.S;
			if(!(St[v] <= St[node] && Fi[node] <= Fi[v]))
			{
				st[v].insert(now);
			}
		}
		sub[v] += sub[u];
	}
	pii cu = *st[v].begin();
	while(St[v] <= St[cu.S] && Fi[cu.S] <= Fi[v])
	{
		st[v].erase(cu);
		cu = *st[v].begin();
	}
	if(last > m)
	{	
		ret += 1ll * cu.F * sub[v];
	}
}

void solve(int mask)
{
	///printf("mask = %d\n", mask);
	ptr = 0;
	ret = 0;
	for(auto v : roots) sz[v] = 1, G[v].clear(), par[v] = v, st[v].clear();
	for(int i = m + 1; i <= m + k; i ++)
	{
		int id = i - m - 1;
		if(mask >> id & 1)
		{
			G[V[i]].push_back(i);
			G[U[i]].push_back(i);
			///printf("E = %d\n", i);
			if(!unify(V[i], U[i]))
			{
			    ///printf("0\n");
			    return;
			}
		}
	}
	for(auto i : vec)
	{
		///printf("%d %d\n", V[i], U[i]);
		if(V[i] == U[i]) continue;
		if(unify(V[i], U[i]))
		{
			G[V[i]].push_back(i);
			G[U[i]].push_back(i);
			///printf("E = %d\n", i);
		}
		else
		{
			st[V[i]].insert({C[i], U[i]});
			st[U[i]].insert({C[i], V[i]});
		}
	}
	pre(ROOT);
	dfs(ROOT);
	tot = max(tot, ret);
	///printf("%lld\n", ret);
}

int main()
{
	scanf("%d%d%d", &n, &m, &k);
	for(int i = 1; i <= m; i ++)
	{
		scanf("%d%d%d", &V[i], &U[i], &C[i]);
		ord[i] = i;
	}
	sort(ord + 1, ord + m + 1, cmp);
	init();
	for(int j = 1; j <= m; j ++)	
	{
		int i = ord[j];
		in1[i] = unify(V[i], U[i]);
	}
	init();
	for(int i = m + 1; i <= m + k; i ++)
	{
		scanf("%d%d", &V[i], &U[i]);
		unify(V[i], U[i]);
	}
	for(int i = 1; i <= n; i ++) { scanf("%d", &T[i]); }
	for(int j = 1; j <= m; j ++)
	{
		int i = ord[j];
		in2[i] = unify(V[i], U[i]);
	}
	init();
	for(int i = 1; i <= m; i ++)
	{
		if(in1[i] > in2[i])
		{
			vec.push_back(i);
		}
		if(in2[i])
		{
			unify(V[i], U[i]);
		}
	}
	for(int i = 1; i <= n; i ++)
	{
		Size[get(i)] += T[i];
		roots.push_back(get(i));
	}
	sort(all(roots));
	roots.resize(unique(all(roots)) - roots.begin());
	sort(all(vec), cmp);
	for(int i = 1; i <= m + k; i ++)
	{
		V[i] = get(V[i]), U[i] = get(U[i]);
	}
	ROOT = get(1);
	for(int mask = 0; mask < 1 << k; mask ++)
	{
		solve(mask);
	}
	printf("%lld\n", tot);
	return 0;
}
/** test corner cases(n = 1?) watch for overflow or minus indices **/

Compilation message

toll.cpp: In function 'int main()':
toll.cpp:140:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  140 |  scanf("%d%d%d", &n, &m, &k);
      |  ~~~~~^~~~~~~~~~~~~~~~~~~~~~
toll.cpp:143:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  143 |   scanf("%d%d%d", &V[i], &U[i], &C[i]);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
toll.cpp:156:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  156 |   scanf("%d%d", &V[i], &U[i]);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~
toll.cpp:159:38: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  159 |  for(int i = 1; i <= n; i ++) { scanf("%d", &T[i]); }
      |                                 ~~~~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 13 ms 21448 KB Output is correct
2 Correct 13 ms 21452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 21448 KB Output is correct
2 Correct 13 ms 21452 KB Output is correct
3 Correct 15 ms 21452 KB Output is correct
4 Correct 15 ms 21516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 21448 KB Output is correct
2 Correct 13 ms 21452 KB Output is correct
3 Correct 15 ms 21452 KB Output is correct
4 Correct 15 ms 21516 KB Output is correct
5 Correct 21 ms 21580 KB Output is correct
6 Correct 16 ms 21680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 21448 KB Output is correct
2 Correct 13 ms 21452 KB Output is correct
3 Correct 15 ms 21452 KB Output is correct
4 Correct 15 ms 21516 KB Output is correct
5 Correct 21 ms 21580 KB Output is correct
6 Correct 16 ms 21680 KB Output is correct
7 Correct 321 ms 30388 KB Output is correct
8 Correct 363 ms 30368 KB Output is correct
9 Correct 363 ms 30392 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 21448 KB Output is correct
2 Correct 13 ms 21452 KB Output is correct
3 Correct 15 ms 21452 KB Output is correct
4 Correct 15 ms 21516 KB Output is correct
5 Correct 21 ms 21580 KB Output is correct
6 Correct 16 ms 21680 KB Output is correct
7 Correct 321 ms 30388 KB Output is correct
8 Correct 363 ms 30368 KB Output is correct
9 Correct 363 ms 30392 KB Output is correct
10 Execution timed out 2573 ms 30432 KB Time limit exceeded
11 Halted 0 ms 0 KB -