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 个点的度数(即有多少个点跟它有边相连) ...
随机推荐
- WPF新手之如何将数据绑定到TreeView
看过许多例子,全是绑定到类的,没人说如何绑定到某个对象,偏偏我这个绝对的新手就是要绑定到一个对象,只能自己摸索了: 首先要将数据绑定到容器,有以下几个默认条件:①元数据必须包装在List或者Obser ...
- 设置Android Studio工程布局文件的默认布局
每次创建新的工程后,布局文件的的布局总是ConstraintLayout,如何更改? 进入Android Studio安装目录,用文本编辑器打开文件plugins\android\lib\templa ...
- idea 破解注册方法总结
注册码(无期限) JetbrainsCrack-2.6.2.jar适用于ideaIU-2017.2.之前版本,若版本较新适用 JetbrainsCrack-2.6.3_proc.jar. 其中Jetb ...
- VK Cup 2016 - Round 1 (Div. 2 Edition) C. Bear and Forgotten Tree 3
C. Bear and Forgotten Tree 3 time limit per test 2 seconds memory limit per test 256 megabytes input ...
- ubuntu主板信息
root:~# sudo dmidecode |grep -A16 "System Information$"sudo: 无法解析主机:phone-TPOWER-X79System ...
- POJ 3750 小孩报数问题 (线性表思想 约瑟夫问题 数组模拟运算的 没用循环链表,控制好下标的指向就很容易了)
小孩报数问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10423 Accepted: 4824 Descripti ...
- SDUT 3035 你猜我猜不猜你猜不猜(字符串 规律性)
你猜我猜不猜你猜不猜 Time Limit: 2000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 In the past 39th annual ACM in ...
- receive和process的过程
(一) receive最终在fuse_kern_chan.c中的fuse_kern_chan_receive函数实现,使用系统调用读取 res = read(fuse_chan_fd(ch), buf ...
- Xcode清楚缓存、清理多余证书路径
Xcode清除缓存.清理多余证书 1.删除Xcode中多余的证书provisioning profile 手动删除: Xcode6 provisioning profile path: ~/Libra ...
- Snowflake算法 ID生成
Snowflake算法 ID生成 http://blog.csdn.net/w200221626/article/details/52064976 使用UUID或者GUID产生的ID没有规则 Snow ...