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 ...
随机推荐
- 边框,元素居中,盒子模型,margin,display,overflow,textarea,float,浮动停止条件,清除浮动影响,margin-top的bug,清除默认样式
边框 solid实线 dotted虚线 dashed点线 盒子在页面中实际的宽高都是5部分组成 宽=borderleft+paddingleft+width+paddingright+borderri ...
- 「SP122」STEVE - Voracious Steve 解题报告
SP122 STEVE - Voracious Steve 题意翻译 Problem Steve和他的一个朋友在玩游戏,游戏开始前,盒子里有 n个甜甜圈,两个人轮流从盒子里抓甜甜圈,每次至少抓 1个, ...
- Android反编译三件套 apktool 、dex2jar、jd-gui
1.还是老话下载三件套(点击下载) 或者自己在百度搜索下载 2.使用apktool反编译apk cd到D:\TESTCODE\android\android反编译三件套目录下 输入java -jar ...
- restframework 认证、权限、频率组件
一.认证 1.表的关系 class User(models.Model): name = models.CharField(max_length=32) pwd = models.CharField( ...
- Java单体应用 - Markdown - 02.基础语法
原文地址:http://www.work100.net/training/monolithic-markdown-basic.html 更多教程:光束云 - 免费课程 基础语法 序号 文内章节 视频 ...
- [洛谷P4097] [HEOI2013] Segment
Description 要求在平面直角坐标系下维护两个操作: 1.在平面上加入一条线段.记第 \(i\) 条被插入的线段的标号为 \(i\) 2.给定一个数 \(k\) ,询问与直线 \(x = k\ ...
- MYGUI3.2改造——与HGE结合,实现资源打包
其实这个有点标题党的意思.MYGUI本身有资源打包的接口,可以实现从内存读取文件. 而HGE也提供了资源打包的功能(不过HGE的资源文件管理比较弱).把MYGUI的接口接到HGE上就可以实现MYGUI ...
- 关于Python类的多继承中的__mro__属性使用的C3算法以及继承顺序解释
刚刚学到类的多继承这个环节,当子类继承多个父类时,调用的父类中的方法具体是哪一个我们无从得知,为此,在Python中有函数__mro__来表示方法解析顺序. 当前Python3.x的类多重继承算法用的 ...
- Client API Object Model - Form Context
FormContext 提供界面或者界面上控件的的引用. 比如说 quick view control, row in an editable grid 等等. Xrm.Page 和 getFormC ...
- Python读取字典(Dictionary)内数据的方法
读取json后,数据类型为字典,对字典内数据的提取又有不同的方法,根据不同的字典类型 上图可以看到有”[]”,”{}” python语言最常见的括号有三种,分别是:小括号( ).中括号[ ]和大括号也 ...