Codeforces Round #617 (Div. 3) F. Berland Beauty 思维

77 篇文章 0 订阅
15 篇文章 1 订阅

传送门

文章目录

题意:

给定一棵树,再给定若干两点最短路之间边权的最小值,让你给树的边权赋值,使得满足给定的条件,如果不存在输出 − 1 -1 1

思路:

观察一个性质,加入经过这条边的路径的最小边权为 w 1 , w 2 , . . , w x w_1,w_2,..,w_x w1,w2,..,wx,那么当前这个边的边权 w n o w ≥ m a x ( w 1 , w 2 , . . . , w x ) w_{now}\ge max(w_1,w_2,...,w_x) wnowmax(w1,w2,...,wx)。所以我们先按照边权从小到大排序,让后每次都直接将 ( a , b ) (a,b) (a,b)之间的边都赋值为 w i w_i wi,最后判断一下是否合法即可。
n n n很小,暴力可过,这里用的 l c a lca lca来暴力的,个人感觉比较方便。

// Problem: F. Berland Beauty
// Contest: Codeforces - Codeforces Round #617 (Div. 3)
// URL: https://codeforces.com/contest/1296/problem/F
// Memory Limit: 256 MB
// Time Limit: 3000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid (tr[u].l+tr[u].r>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;

//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;

const int N=5010,M=N*2,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;

int n,m;
PII p[N];
int e[M],ne[M],w[M],h[N],idx;
int depth[N],fa[N][20];
int g[N][N],flag;
struct Node
{
	int a,b,w;
	bool operator < (const Node &W) const 
	{
		return w<W.w;
	}
}q[N];

void add(int a,int b)
{
	e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}

void dfs_dep(int u,int f)
{
	depth[u]=depth[f]+1;
	fa[u][0]=f;
	for(int i=1;i<=16;i++) fa[u][i]=fa[fa[u][i-1]][i-1];
	for(int i=h[u];~i;i=ne[i])
	{
		int j=e[i];
		if(j==f) continue;
		dfs_dep(j,u);
	}
}

int lca(int a,int b)
{
	if(depth[a]<depth[b]) swap(a,b);
	for(int i=16;i>=0;i--)
		if(depth[fa[a][i]]>=depth[b])
			a=fa[a][i];
	if(a==b) return a;
	for(int i=16;i>=0;i--)
		if(fa[a][i]!=fa[b][i])
			a=fa[a][i],b=fa[b][i];
	return fa[a][0];
}

bool check()
{
	for(int i=1;i<=m;i++)
	{
		int a=q[i].a,b=q[i].b,c=q[i].w;
		int f=lca(a,b);
		int mi=INF;
		while(a!=f)
		{
			if(g[a][fa[a][0]]) mi=min(mi,g[a][fa[a][0]]);
			a=fa[a][0];
		}
		while(b!=f)
		{
			if(g[b][fa[b][0]]) mi=min(mi,g[b][fa[b][0]]);
			b=fa[b][0];
		}
		if(mi!=c) return false;
	}
	return true;
}

int main()
{
//	ios::sync_with_stdio(false);
//	cin.tie(0);

	memset(h,-1,sizeof(h));
	scanf("%d",&n);
	for(int i=1;i<=n-1;i++) 
	{
		int a,b; scanf("%d%d",&a,&b);
		add(a,b); add(b,a);
		p[i]={a,b};
	}
	dfs_dep(1,0);
	scanf("%d",&m);
	for(int i=1;i<=m;i++)
	{
		int a,b,c; scanf("%d%d%d",&a,&b,&c);
		q[i]={a,b,c};
	}
	sort(q+1,q+1+m);
	for(int i=1;i<=m;i++)
	{
		int a=q[i].a,b=q[i].b,c=q[i].w;
		int f=lca(a,b);
		while(a!=f)
		{
			g[a][fa[a][0]]=c;
			g[fa[a][0]][a]=c;
			a=fa[a][0];
		}
		while(b!=f)
		{
			g[b][fa[b][0]]=c;
			g[fa[b][0]][b]=c;
			b=fa[b][0];
		}
	}
	if(check()) 
		for(int i=1;i<=n-1;i++) printf("%d ",g[p[i].X][p[i].Y]==0? 1:g[p[i].X][p[i].Y]);
	else puts("-1");







	return 0;
}
/*

*/


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值