题目链接: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的更多相关文章

  1. [Codeforces 1005F]Berland and the Shortest Paths(最短路树+dfs)

    [Codeforces 1005F]Berland and the Shortest Paths(最短路树+dfs) 题面 题意:给你一个无向图,1为起点,求生成树让起点到其他个点的距离最小,距离最小 ...

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

    题意: 给一棵树,边权未知,现在给m组约束,每组约束给出从u到v路径中的最小值,现在让你给出一组边权,使得符合之前的约束,不能给出输出-1 思路: 因为n较小,对于每组约束我们可以直接暴力修改路径上的 ...

  3. CodeForces 567B Berland National Library

    Description Berland National Library has recently been built in the capital of Berland. In addition, ...

  4. Codeforces B - Berland National Library

    B. Berland National Library time limit per test 1 second memory limit per test 256 megabytes input s ...

  5. AC日记——codeforces Ancient Berland Circus 1c

    1C - Ancient Berland Circus 思路: 求出三角形外接圆: 然后找出三角形三条边在小数意义下的最大公约数; 然后n=pi*2/fgcd; 求出面积即可: 代码: #includ ...

  6. CodeForces - 1073D Berland Fair

    XXI Berland Annual Fair is coming really soon! Traditionally fair consists of nnbooths, arranged in ...

  7. CodeForces 567B Berland National Library hdu-5477 A Sweet Journey

    这类题一个操作增加多少,一个操作减少多少,求最少刚开始为多少,在中途不会出现负值,模拟一遍,用一个数记下最大的即可 #include<cstdio> #include<cstring ...

  8. Codeforces 1189F. Array Beauty

    传送门 首先可以注意到序列里面元素的顺序对答案是没有影响的,所以二话不说先排序再看看怎么搞 考虑枚举每种子序列可能产生的贡献并算一下产生这个贡献的子序列有多少 考虑设 $F(x)$ 表示选择的元素差值 ...

  9. [CF百场计划]Codeforces Round #617 (Div. 3)

    A. Array with Odd Sum Description You are given an array \(a\) consisting of \(n\) integers. In one ...

随机推荐

  1. Linux常用命令大全(二)

    Linux常用命令大全(二) cp命令 将源文件或目录复制到目标文件或目录中 注:如果是目录,需要使用-r选项 -d 复制时保留文件链接 -f 如果现存的目标文件不能打开,则删除并重试 -i 在覆盖目 ...

  2. 虚拟DOM学习与总结

    虚拟DOM 虚拟DOM简而言之就是,用JS去按照DOM结构来实现的树形结构对象,一般称之为虚拟节点(VNode) 优点:解决浏览器性能问题 ,真实DOM频繁排版与重绘的效率是相当低的,虚拟DOM进行频 ...

  3. 跌宕起伏的java帝国史,剖析谷歌甲骨文长达8年的版权战争

    这篇博文是我在B站上发的一个科普java的视频文案整理,本来发过一次了,但是有几种不严谨的地方只能删掉重新发了一下,内容如标题,感兴趣的码农朋友可以观看视频的版本,欢迎提提意见啥的,感谢~https: ...

  4. 现代主流框架路由原理 hash、history的底层原理

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. Eclipse配置运行内存

    对于一些大的项目,运行时等待过长可能出现内存溢出现象,需要重新配置IDE运行内存大小,如下: 1.配置Eclipse.ini 如图再改大点: -Xms512m-Xmx512m 2.JDK内存扩大 Wc ...

  6. docker-tmpfs挂载

    使用tmpfs挂载 卷和绑定装置允许在主机和容器之间共享文件,以便即使在容器停止后也可以保留数据. 如果你在Linux上运行Docker,你有第三个选择:tmpfs mounts.使用tmpfs装载创 ...

  7. 28.python操作excel表格(xlrd/xlwt)

    python读excel——xlrd 这个过程有几个比较麻烦的问题,比如读取日期.读合并单元格内容.下面先看看基本的操作: 首先读一个excel文件,有两个sheet,测试用第二个sheet,shee ...

  8. 解决httpclient设置代理ip之后请求无响应的问题

    httpclient这个工具类对于大家来说应该都不陌生吧,最近在使用过程中出现了碰到一个棘手的问题,当请求的接口地址由http变成https之后,程序执行到 httpClient.execute(ht ...

  9. Java代码调用Shell脚本并传入参数实现DB2数据库表导出到文件

    本文通过Java代码调用Shell脚本并传入参数实现DB2数据库表导出到文件,代码如下: import java.io.File; import java.io.IOException; import ...

  10. 浅析PHP类的自动加载和命名空间

    php是使用require(require_once)和include(include_once)关键字加载类文件.但是在实际的开发工程中我们基本上不会去使用这些关键字去加载类. 因为这样做会使得代码 ...