Codeforces 1296F Berland Beauty
题目链接:http://codeforces.com/problemset/problem/1296/F
思路:
1————2————3————4————5————6
1->3 2
2->4 3
3->5 3
4->6 5
题目说 (u->v w)途中所有边 e1,e2,e3,...en∈E,满足任意|ex| >= w(ex∈E),
上面图中 3->5 3 4->6 5,说明(|e(4->5)| >= 3 && |e(4->5)| >= 5) ==> |e(4->5)| >= 5。
通过这个想法,我们可以把边以(w)排序,大到小,先染色w大的,w小的不能去重新染色w大的,只能
染色未被染色的部分或者w相同的部分,例如(2->4)(3->5)w都是3,那么,如果某次(u->v w)无法染色,
说明就是"-1".
用了lca去优化(u->v)的寻路过程。
#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
#include <cstdio>
using namespace std; const int N = (int)5e3+,INF = (int)1e6;
struct node{
int to,nxt;
}e[N<<];
struct Info{
int u,v,w;
bool friend operator<(const Info& a,const Info& b){
return a.w > b.w;
}
}info[N];
map<pair<int,int>, int > mp;//边
pair<int,int > pii;
vector<pair<int,int > > vii;
int n,m,u,v,w,tot,tim;
int head[N],dfn[N],fa[N]; inline void add(int u,int v){
e[tot].to = v; e[tot].nxt = head[u]; head[u] = tot++;
e[tot].to = u; e[tot].nxt = head[v]; head[v] = tot++;
} void dfs_dfn(int now,int pre){
dfn[now] = ++tim;
for(int o = head[now]; ~o; o = e[o].nxt){
if(e[o].to == pre) continue;
dfs_dfn(e[o].to,now);
}
} void dfs_mp(int now,int pre){
for(int o = head[now]; ~o; o = e[o].nxt){
int to = e[o].to;
if(to == pre) continue;
fa[dfn[to]] = dfn[now];
mp[make_pair(dfn[now],dfn[to])] = INF;//初始化边INF,把边的两点转化为dfn,总是以dnf小的在first位置
//输出答案的时候,通过dfn[u],dfn[v]去得到这条边
dfs_mp(e[o].to,now);
}
} void show(){
for(int i = ; i <= n; ++i)
printf("u = %d dfn = %d\n",i,dfn[i]);
for(int i = ; i <= m; ++i) cout << info[i].w << endl;
for(int i = ; i <= n; ++i)
printf("u = %d fa = %d\n",i,fa[dfn[i]]);
} bool lca(int u,int v,int w){
int u_dfn = dfn[u],v_dfn = dfn[v],tmp_w;
bool ok = ;
while(u_dfn != v_dfn){
while(u_dfn > v_dfn){
pii.first = fa[u_dfn]; pii.second = u_dfn;
tmp_w = mp[pii];
if(tmp_w == INF || tmp_w == w){//未被染色,或者w相同
ok = ;
mp[pii] = w;
}
u_dfn = pii.first;
}
while(u_dfn < v_dfn){
pii.first = fa[v_dfn]; pii.second = v_dfn;
tmp_w = mp[pii];
if(tmp_w == INF || tmp_w == w){
ok = ;
mp[pii] = w;
}
v_dfn = pii.first;
}
}
return ok;
} void solve(){
for(int i = ; i <= n; ++i) head[i] = -; tot = ;
for(int i = ; i <= n-; ++i){
scanf("%d%d",&u,&v);
vii.push_back(make_pair(u,v));
add(u,v);
}
dfs_dfn(,);//初始化dfn序
fa[] = ;
dfs_mp(,);//初始化边长和fa[]数组
scanf("%d",&m);
for(int i = ; i <= m; ++i){
scanf("%d%d%d",&info[i].u,&info[i].v,&info[i].w);
} sort(info+,info++m);//边按W排序
for(int i = ; i <= m; ++i){
if(lca(info[i].u,info[i].v,info[i].w)) continue;
printf("-1\n"); return;
}
int l = vii.size();
int dfn1,dfn2;
for(int i = ; i < l; ++i){
dfn1 = dfn[vii[i].first];
dfn2 = dfn[vii[i].second];
if(dfn1 > dfn2) swap(dfn1,dfn2);
pii.first = dfn1; pii.second = dfn2;
printf("%d ",mp[pii]);
}cout << endl;
} int main(){ scanf("%d",&n);
solve(); return ;
}
Codeforces 1296F Berland Beauty的更多相关文章
- [Codeforces 1005F]Berland and the Shortest Paths(最短路树+dfs)
[Codeforces 1005F]Berland and the Shortest Paths(最短路树+dfs) 题面 题意:给你一个无向图,1为起点,求生成树让起点到其他个点的距离最小,距离最小 ...
- Codeforces Round #617 (Div. 3)F. Berland Beauty
题意: 给一棵树,边权未知,现在给m组约束,每组约束给出从u到v路径中的最小值,现在让你给出一组边权,使得符合之前的约束,不能给出输出-1 思路: 因为n较小,对于每组约束我们可以直接暴力修改路径上的 ...
- CodeForces 567B Berland National Library
Description Berland National Library has recently been built in the capital of Berland. In addition, ...
- Codeforces B - Berland National Library
B. Berland National Library time limit per test 1 second memory limit per test 256 megabytes input s ...
- AC日记——codeforces Ancient Berland Circus 1c
1C - Ancient Berland Circus 思路: 求出三角形外接圆: 然后找出三角形三条边在小数意义下的最大公约数; 然后n=pi*2/fgcd; 求出面积即可: 代码: #includ ...
- CodeForces - 1073D Berland Fair
XXI Berland Annual Fair is coming really soon! Traditionally fair consists of nnbooths, arranged in ...
- CodeForces 567B Berland National Library hdu-5477 A Sweet Journey
这类题一个操作增加多少,一个操作减少多少,求最少刚开始为多少,在中途不会出现负值,模拟一遍,用一个数记下最大的即可 #include<cstdio> #include<cstring ...
- Codeforces 1189F. Array Beauty
传送门 首先可以注意到序列里面元素的顺序对答案是没有影响的,所以二话不说先排序再看看怎么搞 考虑枚举每种子序列可能产生的贡献并算一下产生这个贡献的子序列有多少 考虑设 $F(x)$ 表示选择的元素差值 ...
- [CF百场计划]Codeforces Round #617 (Div. 3)
A. Array with Odd Sum Description You are given an array \(a\) consisting of \(n\) integers. In one ...
随机推荐
- 从零开始Go语言-GoLand(编译器)-Windows(平台)
本文章适合那些想入门Go语言,却又不知道如何搭建自己的第一个HelloWorld的同学. 推荐几个Go语言相关学习网站: C语言中文网: http://c.biancheng.net/golang/ ...
- Git 合并多次提交
在合并分支的时候,希望将多次提交合并成一个,然后再 cherry-pick 到主分支. 合并分支 develop 分支做开发,可能会进行多次提交,但是在发布或者进行 PR 的时候,我们只希望看到一次提 ...
- 在Windows Server 2003中搭建DNS服务器
1.安装Windows Server 2003虚拟机 准备好Windows Server 2003的镜像:http://www.downza.cn/soft/184944.html 2.Windows ...
- Web及网络基础学习(一)
---恢复内容开始--- 2019.10.16 1.TCP.IP分层 应用层.网络层.传输层.数据链路层 2.各层讲解 应用层 决定了向用户提供应用服务时通信的活动.例如FTP(File Trans ...
- 【php学习】图片处理三步走
前两天要对一张图片进行处理,其实很简单,就是在图片上加上字符串,一个图片而已,但是自己如同得了短暂性失忆似的,图片操作的函数一个都想不起来.所以就抽空整理了一下图片操作函数. 1. 创建画布 从文件中 ...
- 【JavaScript 基础知识】一篇关于 JavaScript 一些知识点的总结
JavaScript 中基础数据类型 数据类型名称 数据类型说明 Undefined 只有一个值,即 undefined ,声明变量的初始值. Null 只有一个值,即 null ,表示空指针, ...
- Excel查找匹配函数的16种方法
作者:高顿初级会计链接:https://zhuanlan.zhihu.com/p/79795779来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 1.普通查找 查找李晓 ...
- JVM内存布局及GC知识
一.JVM运行时内存布局 按java 8虚拟机规范的原始表达:(jvm)Run-Time Data Areas, 暂时翻译为"jvm运行时内存布局". 从概念上大致分为6个(逻辑) ...
- c语言-输出圆形
#include<stdio.h> #define high 100//定义界面大小 #define wide 100 void Circle(int ridus) //确定坐标 {int ...
- 【转】document.form.action,表单分向提交
document.form.action,表单分向提交,javascript提交表单 同一个表单可以根据用户的选择,提交给不同的后台处理程序.即,表单的分向提交.如,在编写论坛程序时,如果我们希望实现 ...