Building Forest CodeForces - 195E
Building Forest CodeForces - 195E
这题意真是难懂啊...话说"An oriented weighted forest is an acyclic weighted digraph in which from each vertex at most one edge goes."这句话到底什么意思...
题意:有n个点要按照1-n的顺序依次插入一个带边权的森林(森林一开始没有任何点、边),第i+1行描述第i个点插入后的操作。如果某一行为(k, v1, x1, v2, x2, ... , vk, xk),那么k表示要连的边的数量,(vj,xj)表示要从第vj个点连一条边到i,其权值为vj到其原来所在树的根节点的路径总长加上xj。(保证数据不产生重边)
做法:看懂了题,就会发现这是一个非常正常(shui)的加权并查集。
#include<cstdio>
#define md 1000000007
typedef long long LL;
LL fa[];
LL hei[];//记录点i到父亲节点的路径总长
LL ans,n;
LL find(LL x)
{
if(fa[x]==x) return x;
LL t=find(fa[x]);
hei[x]=(hei[fa[x]]+hei[x])%md;
fa[x]=t;
return fa[x];
}
int main()
{
LL i,j,k,v,x,f1;
scanf("%I64d",&n);
for(i=;i<=n;i++)
fa[i]=i;
for(i=;i<=n;i++)
{
scanf("%I64d",&k);
for(j=;j<=k;j++)
{
scanf("%I64d%I64d",&v,&x);
f1=find(v);
//if(f1==i) continue;
fa[f1]=i;
hei[f1]=(x+hei[v])%md;
ans=(ans+hei[f1])%md;
}
}
printf("%I64d",(ans+md)%md);
return ;
}
曾经犯过的错:
1.答案未取模,输出了负数(写了两次,错了两次)
2.误区:12行
#include<cstdio>
#define md 1000000007
typedef long long LL;
LL fa[];
LL hei2[];//记录点i连向其父亲节点的边的权值
LL hei[];//记录点i到父亲节点的路径总长
LL ans,n;
LL find(LL x)
{
if(fa[x]==x) return x;
LL t=find(fa[x]);
hei[x]=(hei[fa[x]]+hei2[x])%md;
fa[x]=t;
return fa[x];
}
int main()
{
LL i,j,k,v,x,f1;
scanf("%I64d",&n);
for(i=;i<=n;i++)
fa[i]=i;
for(i=;i<=n;i++)
{
scanf("%I64d",&k);
for(j=;j<=k;j++)
{
scanf("%I64d%I64d",&v,&x);
f1=find(v);
if(f1==i) continue;
fa[f1]=i;
hei2[f1]=(x+hei[v])%md;
}
}
for(i=;i<=n;i++)
ans=(ans+hei2[i]+md)%md;
printf("%I64d",ans);
return ;
}
Building Forest CodeForces - 195E的更多相关文章
- Codeforces Round #123 (Div. 2)
A. Let's Watch Football 记时间为\(t\), 则\(bt+(b-a)c>=0\),可得\[t \ge \frac{c(a-b)}{b}\] B. After Traini ...
- codeforces285C
Building Permutation CodeForces - 285C Permutation p is an ordered set of integers p1, p2, ..., p ...
- 多校联训 DS 专题
CF1039D You Are Given a Tree 容易发现,当 \(k\) 不断增大时,答案不断减小,且 \(k\) 的答案不超过 \(\lfloor\frac {n}{k}\rfloor\) ...
- Codeforces Round #192 (Div. 1) B. Biridian Forest 暴力bfs
B. Biridian Forest Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/329/pr ...
- [Codeforces Round #192 (Div. 2)] D. Biridian Forest
D. Biridian Forest time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 图论/位运算 Codeforces Round #285 (Div. 2) C. Misha and Forest
题目传送门 /* 题意:给出无向无环图,每一个点的度数和相邻点的异或和(a^b^c^....) 图论/位运算:其实这题很简单.类似拓扑排序,先把度数为1的先入对,每一次少一个度数 关键在于更新异或和, ...
- 水题 Codeforces Round #285 (Div. 2) C. Misha and Forest
题目传送门 /* 题意:给出无向无环图,每一个点的度数和相邻点的异或和(a^b^c^....) 图论/位运算:其实这题很简单.类似拓扑排序,先把度数为1的先入对,每一次少一个度数 关键在于更新异或和, ...
- 【codeforces 755C】PolandBall and Forest
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- codeforces 501C. Misha and Forest 解题报告
题目链接:http://codeforces.com/problemset/problem/501/C 题目意思:有 n 个点,编号为 0 - n-1.给出 n 个点的度数(即有多少个点跟它有边相连) ...
随机推荐
- device not managed by NetworkManager
Bringing up interface eth0:Error:Connection activation failed:Device not managed by NetworkManager ...
- ViewGroup如何分发事件
dispatchTouchEvent事件派发显示隧道方式.再是冒泡方式隧道方式传递,直道某一个元素消耗此事件,由上至下逐层分发视图.冒泡方式传递,当某个视图消耗事件后其return boolean 是 ...
- SyntaxError:Strict mode does not allow function declaration in a lexically nested statement.
问题描述 使用react-native init创建了一个新项目,在package.json中使用的react-native的版本如下: "dependencies": { & ...
- stm8 停机模式与外部中断唤醒中一个小问题
做了一个简单的项目,电路板使用电池供电,需要系统在待机时低功耗.而对外接口只有4个按键,也就是唤醒必须要通过这四个按键. 系统功能就不介绍了,只给出进入低功耗的代码和退出低功耗的代码. 使用芯片为st ...
- Caused by: java.lang.IllegalArgumentException: Malformed \uxxxx encoding.
application.properties中不能含有\符号. 只要遇到就会报这个错误. 解决方式:将application.properties中的 \ 改为 \\ 或者 /
- 一步一步学Silverlight 2系列(15):数据与通信之ASMX
概述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...
- java类成员访问权限总结(private,default,protected,public)
- python(一):multiprocessing——死锁
前言近年来,使用python的人越来越多,这得益于其清晰的语法.低廉的入门代价等因素.尽管python受到的关注日益增多,但python并非完美,例如被人诟病最多的GIL(值得注意的是,GIL并非py ...
- RDA PQ工具使用 (Adi Analysis)
PQ工具“ColorAdjustTool.exe”,请注意芯片的选择: RDA512C选择533 RDA8501选择331 RDA8503选择131 工模菜单 COLOR LUT: R/G/B/Y/ ...
- 文件的创建,读取,写入,修改,删除---python入门
转自:http://blog.163.com/jackylau_v/blog/static/175754040201181505158356/ 一.用Python创建一个新文件,内容是从0到9的整数, ...