답안 #351940

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
351940 2021-01-20T09:56:48 Z arnold518 통행료 (APIO13_toll) C++14
78 / 100
1389 ms 163844 KB
#include <bits/stdc++.h>
using namespace std;

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

const int MAXN = 1e5;
const int MAXM = 3e5;
const int MAXK = 20;

int N, M, K;

struct Edge
{
	int u, v, w;
};
Edge E[MAXM+10];
pii A[MAXK+10];
ll B[MAXN+10];

struct UF
{
	int par[MAXN+10];
	void init()
	{
		for(int i=1; i<=N; i++)
		{
			par[i]=i;
		}
	}
	int Find(int x)
	{
		if(x==par[x]) return x;
		return par[x]=Find(par[x]);
	}
	void Union(int x, int y)
	{
		x=Find(x); y=Find(y);
		par[x]=y;
	}
}uf, uf2;

ll ans, val=0;
vector<pii> adj[MAXN+10], adj2[MAXN+10];

ll sz[MAXN+10];
vector<int> ST;

int P[MAXK+10];

void dfs(int now, int bef, int k)
{
	sz[now]=B[now];
	for(auto nxt : adj[now])
	{
		if(nxt.first==bef) continue;
		int t=0;
		if(nxt.second!=-1) t=P[nxt.second];
		dfs(nxt.first, now, t);
		sz[now]+=sz[nxt.first];
	}
	//printf("?%d : %d\n", now, sz[now]);
	val+=sz[now]*k;
}

int dfs2(int now, int bef, int k)
{
	if(now==k) return 1;
	for(auto nxt : adj[now])
	{
		if(nxt.first==bef) continue;
		if(nxt.second!=-1 && P[nxt.second]==0) ST.push_back(nxt.second);
		if(dfs2(nxt.first, now, k)) return 1;
		if(nxt.second!=-1 && P[nxt.second]==0) ST.pop_back();
	}
	return 0;
}

int col[MAXN+10];
ll D[MAXN+10];

int par[MAXN+10][30], dep[MAXN+10], L[MAXN+10], cnt;

void dfs3(int now, int bef, int d)
{
	L[now]=++cnt;
	par[now][0]=bef;
	dep[now]=d;
	for(auto nxt : adj2[now])
	{
		if(nxt.first==bef) continue;
		dfs3(nxt.first, now, d+1);
	}
}

int lca(int u, int v)
{
	if(dep[u]>dep[v]) swap(u, v);
	for(int i=20; i>=0; i--) if(dep[u]<=dep[par[v][i]]) v=par[v][i];
	if(u==v) return u;
	for(int i=20; i>=0; i--) if(par[u][i]!=par[v][i]) u=par[u][i], v=par[v][i];
	return par[u][0];
}

int par2[MAXN+10];
int dep2[MAXN+10];
int val2[MAXN+10];

void dfs4(int now, int bef, int d, int k)
{
	par2[now]=bef;
	dep2[now]=d;
	val2[now]=k;
	for(pii nxt : adj[now])
	{
		if(nxt.first==bef) continue;
		dfs4(nxt.first, now, d+1, nxt.second);
	}
}

int main()
{
	scanf("%d%d%d", &N, &M, &K);
	for(int i=1; i<=M; i++) scanf("%d%d%d", &E[i].u, &E[i].v, &E[i].w);
	for(int i=0; i<K; i++) scanf("%d%d", &A[i].first, &A[i].second);
	for(int i=1; i<=N; i++) scanf("%d", &B[i]);

	sort(E+1, E+M+1, [&](const Edge &p, const Edge &q)
	{
		return p.w<q.w;
	});

	uf.init();
	vector<Edge> EE;
	for(int i=1; i<=M; i++)
	{
		if(uf.Find(E[i].u)==uf.Find(E[i].v)) continue;
		uf.Union(E[i].u, E[i].v);
		EE.push_back(E[i]);
	}
	for(int i=0; i<EE.size(); i++) E[i+1]=EE[i];
	M=EE.size();
	EE.clear();
	assert(M==N-1);

	for(int i=1; i<=M; i++)
	{
		adj2[E[i].u].push_back({E[i].v, E[i].w});
		adj2[E[i].v].push_back({E[i].u, E[i].w});
	}

	dfs3(1, 1, 1);
	for(int i=1; i<=20; i++) for(int j=1; j<=N; j++) par[j][i]=par[par[j][i-1]][i-1];

	vector<int> VV;
	VV.push_back(1);
	for(int i=0; i<K; i++)
	{
		VV.push_back(A[i].first);
		VV.push_back(A[i].second);
	}
	sort(VV.begin(), VV.end(), [&](const int &p, const int &q)
	{
		return L[p]<L[q];
	});
	VV.erase(unique(VV.begin(), VV.end()), VV.end());

	int t=VV.size();
	for(int i=0; i+1<t; i++) VV.push_back(lca(VV[i], VV[i+1]));

	sort(VV.begin(), VV.end(), [&](const int &p, const int &q)
	{
		return L[p]<L[q];
	});
	VV.erase(unique(VV.begin(), VV.end()), VV.end());
	sort(VV.begin(), VV.end());

	uf.init();
	for(auto it : VV) col[it]=it;

	for(int i=1; i<=N; i++) D[i]=B[i];
	for(int i=1; i<=M; i++)
	{
		if(col[uf.Find(E[i].u)] && col[uf.Find(E[i].v)])
		{
			EE.push_back(E[i]);
		}
		else
		{
			int p=uf.Find(E[i].u), q=uf.Find(E[i].v);
			D[p]+=D[q];
			uf.par[q]=p;
			col[p]|=col[q];
		}
	}

	for(auto &it : EE)
	{
		it.u=lower_bound(VV.begin(), VV.end(), col[uf.Find(it.u)])-VV.begin()+1;
		it.v=lower_bound(VV.begin(), VV.end(), col[uf.Find(it.v)])-VV.begin()+1;
	}
	for(int i=0; i<EE.size(); i++) E[i+1]=EE[i];
	M=EE.size();

	for(int i=0; i<K; i++)
	{
		A[i].first=lower_bound(VV.begin(), VV.end(), A[i].first)-VV.begin()+1;
		A[i].second=lower_bound(VV.begin(), VV.end(), A[i].second)-VV.begin()+1;
	}

	for(int i=0; i<VV.size(); i++)
	{
		B[i+1]=D[uf.Find(VV[i])];
	}
	N=VV.size();
/*
	printf("VV\n");
	for(auto it : VV) printf("%d ", it); printf("\n");
	printf("E\n");
	for(int i=1; i<=M; i++) printf("%d %d %d\n", E[i].u, E[i].v, E[i].w);
	printf("B\n");
	for(int i=1; i<=N; i++) printf("%lld ", B[i]); printf("\n");
	printf("A\n");
	for(int i=0; i<K; i++) printf("%d %d\n", A[i].first, A[i].second);
*/

	assert(N<=100);
	for(int i=1; i<(1<<K); i++)
	{
		vector<int> V;
		for(int j=0; j<K; j++)
		{
			if(i&(1<<j))
			{
				V.push_back(j);
			}
		}

		uf.init();
		bool flag=true;
		for(auto it : V)
		{
			if(uf.Find(A[it].first)==uf.Find(A[it].second))
			{
				flag=false;
				break;
			}
			uf.Union(A[it].first, A[it].second);
			adj[A[it].first].push_back({A[it].second, it});
			adj[A[it].second].push_back({A[it].first, it});
		}

		if(!flag)
		{
			for(int i=1; i<=N; i++) adj[i].clear();
			continue;
		}

		for(int j=0; j<K; j++) P[j]=0;
		for(int j=1; j<=M; j++)
		{
			if(uf.Find(E[j].u)==uf.Find(E[j].v))
			{

			}
			else
			{
				uf.Union(E[j].u, E[j].v);
				adj[E[j].u].push_back({E[j].v, -1});
				adj[E[j].v].push_back({E[j].u, -1});
				//printf("!%d %d %d\n", E[j].u, E[j].v, 0);
			}
		}

		dfs4(1, 1, 1, -1);

		if(K>15) continue;

		uf.init();
		uf2.init();

		for(auto it : V) uf.Union(A[it].first, A[it].second);
		for(int j=1; j<=M; j++)
		{
			if(uf.Find(E[j].u)==uf.Find(E[j].v))
			{
				int u=E[j].u, v=E[j].v;
				u=uf2.Find(u); v=uf2.Find(v);
				while(u!=v)
				{
					if(dep2[u]>dep2[v]) swap(u, v);
					P[val2[v]]=E[j].w;
					uf2.Union(v, par2[v]);
					v=uf2.Find(v);
				}
			}
			else
			{
				uf.Union(E[j].u, E[j].v);
				int uu=uf2.Find(E[j].u), vv=uf2.Find(E[j].v);
				if(dep2[uu]>dep2[vv]) swap(uu, vv);
				uf2.Union(vv, uu);
			}
		}

		val=0;
		dfs(1, 1, 0);
		ans=max(ans, val);

		for(int i=1; i<=N; i++) adj[i].clear();
	}
	printf("%lld\n", ans);
}

Compilation message

toll.cpp: In function 'int main()':
toll.cpp:127:34: warning: format '%d' expects argument of type 'int*', but argument 2 has type 'll*' {aka 'long long int*'} [-Wformat=]
  127 |  for(int i=1; i<=N; i++) scanf("%d", &B[i]);
      |                                 ~^   ~~~~~
      |                                  |   |
      |                                  |   ll* {aka long long int*}
      |                                  int*
      |                                 %lld
toll.cpp:142:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Edge>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  142 |  for(int i=0; i<EE.size(); i++) E[i+1]=EE[i];
      |               ~^~~~~~~~~~
toll.cpp:203:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Edge>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  203 |  for(int i=0; i<EE.size(); i++) E[i+1]=EE[i];
      |               ~^~~~~~~~~~
toll.cpp:212:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  212 |  for(int i=0; i<VV.size(); i++)
      |               ~^~~~~~~~~~
toll.cpp:124:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  124 |  scanf("%d%d%d", &N, &M, &K);
      |  ~~~~~^~~~~~~~~~~~~~~~~~~~~~
toll.cpp:125:31: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  125 |  for(int i=1; i<=M; i++) scanf("%d%d%d", &E[i].u, &E[i].v, &E[i].w);
      |                          ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
toll.cpp:126:30: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  126 |  for(int i=0; i<K; i++) scanf("%d%d", &A[i].first, &A[i].second);
      |                         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
toll.cpp:127:31: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  127 |  for(int i=1; i<=N; i++) scanf("%d", &B[i]);
      |                          ~~~~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5248 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5248 KB Output is correct
3 Correct 5 ms 5100 KB Output is correct
4 Correct 5 ms 5100 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5248 KB Output is correct
3 Correct 5 ms 5100 KB Output is correct
4 Correct 5 ms 5100 KB Output is correct
5 Correct 10 ms 5356 KB Output is correct
6 Correct 7 ms 5356 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5248 KB Output is correct
3 Correct 5 ms 5100 KB Output is correct
4 Correct 5 ms 5100 KB Output is correct
5 Correct 10 ms 5356 KB Output is correct
6 Correct 7 ms 5356 KB Output is correct
7 Correct 397 ms 28408 KB Output is correct
8 Correct 314 ms 28384 KB Output is correct
9 Correct 315 ms 28340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5100 KB Output is correct
2 Correct 4 ms 5248 KB Output is correct
3 Correct 5 ms 5100 KB Output is correct
4 Correct 5 ms 5100 KB Output is correct
5 Correct 10 ms 5356 KB Output is correct
6 Correct 7 ms 5356 KB Output is correct
7 Correct 397 ms 28408 KB Output is correct
8 Correct 314 ms 28384 KB Output is correct
9 Correct 315 ms 28340 KB Output is correct
10 Runtime error 1389 ms 163844 KB Execution killed with signal 9
11 Halted 0 ms 0 KB -