Submission #746454

# Submission time Handle Problem Language Result Execution time Memory
746454 2023-05-22T13:10:14 Z denniskim Toll (APIO13_toll) C++17
78 / 100
2500 ms 29744 KB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef __int128 lll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pld;
#define MAX 9223372036854775807LL
#define MIN -9223372036854775807LL
#define INF 0x3f3f3f3f3f3f3f3f
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cout << fixed; cout.precision(10);
#define sp << " "
#define en << "\n"
#define compress(v) sort(v.begin(), v.end()), v.erase(unique(v.begin(), v.end()), v.end())

struct edge
{
	ll u, v, cost;
	
	bool operator < (const edge &xx) const
	{
		return cost < xx.cost;
	}
};

ll n, m, K;
ll all, bll, cll;
vector<edge> edg;
vector< pair<pll, ll> > mst[3];
set< pair<pll, ll> > st;
pll ne[100010];
ll a[100010];
ll p[100010], ra[100010];
vector<ll> V;
vector< pair<pll, ll> > vec[30];
ll sum[100010];
ll siz;
ll grp[100010];
ll chk[30], last[30];
ll dp[30];
ll ans;
ll dep[30];

ll ffind(ll here)
{
	if(here == p[here])
		return here;
	
	return p[here] = ffind(p[here]);
}

void uunion(ll X, ll Y)
{
	X = ffind(X);
	Y = ffind(Y);
	
	if(X == Y)
		return;
	
	if(ra[X] < ra[Y])
		p[X] = Y;
	else if(ra[X] > ra[Y])
		p[Y] = X;
	
	else
	{
		p[X] = Y;
		ra[Y]++;
	}
}

void MST(ll num)
{
	for(ll i = 1 ; i <= n ; i++)
		p[i] = i, ra[i] = 0;
	
	sort(edg.begin(), edg.end());
	
	for(auto &i : edg)
	{
		if(ffind(i.u) == ffind(i.v))
			continue;
		
		uunion(i.u, i.v);
		mst[num].push_back({{min(i.u, i.v), max(i.u, i.v)}, i.cost});
	}
}

void dfs(ll here, ll pa)
{
	last[here] = pa;
	
	for(auto &i : vec[here])
	{
		if(i.fi.fi == pa)
			continue;
		
		dfs(i.fi.fi, here);
	}
}

void add_edge(ll I)
{
	/*cout << "Add Edge" sp << ne[I].fi sp << ne[I].se en;
	cout << "Before : ";
	
	for(ll i = 1 ; i <= siz ; i++)
	{
		for(auto &j : vec[i])
			cout << i sp << "->" << j.fi.fi sp << j.fi.se sp << j.se en;
	}*/
	
	ll uu = ne[I].fi;
	ll vv = ne[I].se;
	
	dfs(uu, 0);
	
	for(ll i = 1 ; i <= siz ; i++)
		chk[i] = 0;
	
	for(ll i = vv ; i != 0 ; i = last[i])
		chk[i] = 1;
	
	vector< pair<pll, pll> > cos;
	
	for(ll i = 1 ; i <= siz ; i++)
	{
		for(auto &j : vec[i])
		{
			if(chk[i] && chk[j.fi.fi])
				cos.push_back({{j.fi.se, j.se}, {i, j.fi.fi}});
		}
	}
	
	sort(cos.begin(), cos.end());
	reverse(cos.begin(), cos.end());
	
	ll bi = -INF;
	
	for(auto &i : cos)
	{
		if(i.fi.se == 0)
			bi = max(bi, i.fi.fi);
	}
	
	if(bi == -INF)
		return;
	
	vector< pair<pll, ll> > help[30];
	
	for(ll i = 1 ; i <= siz ; i++)
	{
		for(auto &j : vec[i])
		{
			if(j.se == 1 && j.fi.se > bi && chk[i] && chk[j.fi.fi])
				help[i].push_back({{j.fi.fi, bi}, j.se});
			else if(j.se == 0 && j.fi.se == bi)
				continue;
			else
				help[i].push_back(j);
		}
	}
	
	help[uu].push_back({{vv, bi}, 1});
	help[vv].push_back({{uu, bi}, 1});
	
	for(ll i = 1 ; i <= siz ; i++)
		vec[i] = help[i];
	
	/*cout << "After : ";
	
	for(ll i = 1 ; i <= siz ; i++)
	{
		for(auto &j : vec[i])
			cout << i sp << "->" << j.fi.fi sp << j.fi.se sp << j.se en;
	}*/
}

void dfs2(ll here, ll pa)
{
	dp[here] = sum[here];
	dep[here] = dep[pa] + 1;
	
	for(auto &i : vec[here])
	{
		if(i.fi.fi == pa)
			continue;
		
		dfs2(i.fi.fi, here);
		dp[here] += dp[i.fi.fi];
	}
}

ll hehe = 0;

void f(ll here)
{
	if(here > K)
	{
		ll gap = 0;
		
		dfs2(grp[1], 0);
		
		for(ll i = 1 ; i <= siz ; i++)
		{
			for(auto &j : vec[i])
			{
				if(j.se == 0)
					continue;
				
				if(dep[j.fi.fi] < dep[i])
					continue;
				
				gap += dp[j.fi.fi] * j.fi.se;
			}
		}
		
		ans = max(ans, gap);
		return;
	}
	
	f(here + 1);
	
	vector< pair<pll, ll> > tmp[30];
	
	for(ll i = 1 ; i <= siz ; i++)
		tmp[i] = vec[i];
	
	add_edge(here);
	
	f(here + 1);
	
	for(ll i = 1 ; i <= siz ; i++)
		vec[i] = tmp[i];
}

int main(void)
{
	fastio
	
	cin >> n >> m >> K;
	
	for(ll i = 1 ; i <= m ; i++)
	{
		cin >> all >> bll >> cll;
		edg.push_back({all, bll, cll});
	}
	
	for(ll i = 1 ; i <= K ; i++)
		cin >> ne[i].fi >> ne[i].se;
	
	for(ll i = 1 ; i <= n ; i++)
		cin >> a[i];
	
	MST(0);
	
	for(ll i = 1 ; i <= K ; i++)
		edg.push_back({ne[i].fi, ne[i].se, 0});
	
	MST(1);
	
	for(auto &i : mst[1])
		st.insert(i);
	
	for(ll i = 1 ; i <= n ; i++)
		p[i] = i, ra[i] = 0;
	
	for(auto &i : mst[0])
	{
		if(st.find(i) == st.end())
			mst[2].push_back(i);
		else
			uunion(i.fi.fi, i.fi.se);
	}
	
	for(ll i = 1 ; i <= n ; i++)
		V.push_back(ffind(i));
	
	compress(V);
	siz = (ll)V.size();
	
	for(ll i = 1 ; i <= n ; i++)
	{
		ll w = ffind(i);
		
		w = lower_bound(V.begin(), V.end(), w) - V.begin() + 1;
		grp[i] = w;
		sum[w] += a[i];
	}
	
	for(auto &i : mst[2])
	{
		ll v1 = ffind(i.fi.fi);
		ll v2 = ffind(i.fi.se);
		
		v1 = lower_bound(V.begin(), V.end(), v1) - V.begin() + 1;
		v2 = lower_bound(V.begin(), V.end(), v2) - V.begin() + 1;
		
		vec[v1].push_back({{v2, i.se}, 0});
		vec[v2].push_back({{v1, i.se}, 0});
	}
	
	/*cout << siz en;
	
	for(ll i = 1 ; i <= siz ; i++)
	{
		for(auto &j : vec[i])
			cout << i sp << "->" << j.fi.fi sp << j.fi.se en;
	}*/
	
	for(ll i = 1 ; i <= K ; i++)
	{
		ne[i].fi = lower_bound(V.begin(), V.end(), ffind(ne[i].fi)) - V.begin() + 1;
		ne[i].se = lower_bound(V.begin(), V.end(), ffind(ne[i].se)) - V.begin() + 1;
	}
	
	/*cout << grp[1] en;
	
	for(ll i = 1 ; i <= K ; i++)
		cout << ne[i].fi sp << ne[i].se en;
	
	for(ll i = 1 ; i <= siz ; i++)
		cout << sum[i] sp;
	
	cout en;
	*/
	f(1);
	
	cout << ans;
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 3 ms 340 KB Output is correct
4 Correct 3 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 3 ms 340 KB Output is correct
4 Correct 3 ms 336 KB Output is correct
5 Correct 5 ms 660 KB Output is correct
6 Correct 4 ms 676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 3 ms 340 KB Output is correct
4 Correct 3 ms 336 KB Output is correct
5 Correct 5 ms 660 KB Output is correct
6 Correct 4 ms 676 KB Output is correct
7 Correct 314 ms 29676 KB Output is correct
8 Correct 346 ms 29716 KB Output is correct
9 Correct 345 ms 29652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 3 ms 340 KB Output is correct
4 Correct 3 ms 336 KB Output is correct
5 Correct 5 ms 660 KB Output is correct
6 Correct 4 ms 676 KB Output is correct
7 Correct 314 ms 29676 KB Output is correct
8 Correct 346 ms 29716 KB Output is correct
9 Correct 345 ms 29652 KB Output is correct
10 Execution timed out 2559 ms 29744 KB Time limit exceeded
11 Halted 0 ms 0 KB -