题目链接: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. ScheduledThreadPoolExecutor中定时周期任务的实现源码分析

    ScheduledThreadPoolExecutor是一个定时任务线程池,相比于ThreadPoolExecutor最大的不同在于其阻塞队列的实现 首先看一下其构造方法: public Schedu ...

  2. 小小知识点(十四)——Adobe photoshop cc 2018中简单抠图的一些基本操作

    一 如何抠图 1. 右键弹出选择工具,随后鼠标左键选择快速选择工具 2.通过点击鼠标,选择想要的区域: Alt+鼠标右键  左右拖动鼠标可调整画笔大小 Alt+鼠标滑轮,可放大或缩小画布大小 ctrl ...

  3. ChromeDriver+Selenium安装

    介绍 Selenium是一个自动化测试工具,利用它我们可以驱动浏览器执行特定的动作,如点击.下拉等操作. ChromeDriver是一个Chrome浏览器驱动,用于驱动Chrome浏览器完成相应的操作 ...

  4. 阿里云ECS单节点Kubernetes部署

    参考资料: kubernetes官网英文版 kubernetes官网中文版 环境.工具 阿里云学生机ECS.Ubuntu.docker.kubectl1.15.4.kubelet1.15.4.kube ...

  5. 用 Serverless 快速搭建个人相册网站

    日常生活中我们经常会拍摄一些视频.照片等,这些文件会占用比较多的存储空间.本文将介绍一种方法:利用 ThumbsUp 工具,结合 Serverless Framework 的 component 快速 ...

  6. React16源码解读:开篇带你搞懂几个面试考点

    引言 如今,主流的前端框架React,Vue和Angular在前端领域已成三足鼎立之势,基于前端技术栈的发展现状,大大小小的公司或多或少也会使用其中某一项或者多项技术栈,那么掌握并熟练使用其中至少一种 ...

  7. 田忌赛马 - dp

    Here is a famous story in Chinese history. That was about 2300 years ago. General Tian Ji was a high ...

  8. SpringSecurity 初始化流程源码

    SpringSecurity 初始化流程源码 本篇主要讲解 SpringSecurity初始化流程的源码部分,包括核心的 springSecurityFilterChain 是如何创建的,以及在介绍哪 ...

  9. WingIDE注册破解方法 CalcActivationCode.py

    1) 安装WingIDE成功后启动,激活时输入license id CN123-12345-12345-12345 2) 点击Continue后弹框,拷贝框中的request code 3) 修改Py ...

  10. pikachu漏洞练习之sql注入

    这里因为实验的时候只记录了一部分所以就展示一部分 1.1.1数字型注入 (1)看到界面发现是查询id功能,没有在url里看到有传参所以应该是post方法提交数据. (2)进行sql注入之前我们最好是先 ...