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 ...
随机推荐
- Codeforces Round #612 (Div. 2)
https://codeforces.com/contest/1287/ A - Angry Students 题意:求A后面的P最长连续有几个? 题解:? int n; char s[200005] ...
- MySQL之非空约束(NOT NULL)
定义: MySQL非空约束(NOT NULL)可以通过 CREATE TABLE 或 ALTER TABLE 语句实现.在表中某个列的定义后加上关键字 NOT NULL 作为限定词,来约束该列的取值不 ...
- SpringBoot 总结篇
时至今日,SpringBoot 系列文章也算是告一段落,回想起当初立flag的情景,仿佛还历历在目.用一个月时间学完 SpringBoot 并整理成文章?又定一些异想天开计划,当时这样 ...
- 08_jquery里面的$(this)和this都什么时候用,有什么区别
当你用的是jquery时,就用$(this),如果是JS,就用this $(this).html( $(this).html() + " BAM! "); 这个里的html()是J ...
- Oracle表空间概述及其基本管理
最近在工作中遇到有同事对Oracle表空间的理解有问题,所以写了这篇文章.我会从概念,管理及特别需要关注的点等几个维度对表空间进行一些介绍.本文以介绍表空间为主,涉及到的其他概念不展开描述.有问题的地 ...
- MapInfo常见数据格式
在MapInfo 中所指的表是单纯的数据表或是图形与数据的结合.一个典型的MapInfo表将主要由*.tab.*.dat.*.wks.*.dbf.*.xls.*.map.*.id.*.ind文件格式组 ...
- 【PCIE-3】---PCIE设备的枚举扫描(经典好文)
前面两个小节大致总结了下PCIE的基本知识,算是扫盲篇吧.本文主要总结PCIE设备的枚举扫描过程,此部分才是PCIE模块的重点,无论是在BIOS下还是系统驱动下都会用到. 按照国际惯例,先列问题: 1 ...
- 《C# 爬虫 破境之道》:第二境 爬虫应用 — 第一节:HTTP协议数据采集
首先欢迎您来到本书的第二境,本境,我们将全力打造一个实际生产环境可用的爬虫应用了.虽然只是刚开始,虽然路漫漫其修远,不过还是有点小鸡冻:P 本境打算针对几大派生类做进一步深耕,包括与应用的结合.对比它 ...
- Java中的equalsIgnoreCase()
在工作中偶然机会看到了equalsIgnoreCase()这个方法,相信大家和我一样equals()是再熟悉不过的了,但是对于equalsIgnoreCase()有点眼生(大神勿喷),所以写了这篇博客 ...
- Kubernetes concepts 系列
kubernetes concepts overview Pod overview Replication Controller Pod Liftcycle Termination Of Pod Re ...