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 个点的度数(即有多少个点跟它有边相连) ...
随机推荐
- 深入浅出 - Android系统移植与平台开发(九)- Android系统system_server及Home启动
3.3 Zygote守护进程与system_server进程 Android的执行环境和Java执行环境有着本质的差别,在Android系统中每一个应用程序都是一独立的进程,当一个进程死掉时,不会影响 ...
- 理解和使用WPF 验证机制
博客 学院 下载 更多 写博客 发布Chat 登录注册 理解和使用WPF 验证机制 原创 2013年06月20日 11:15:37 7404 首先建立一个demo用以学习和实验WPF Data Val ...
- top load average
负载均值 等待运行的进程数
- GitExtensions 官方手册(英文)
在线版本(最新版):https://git-extensions-documentation.readthedocs.io/en/latest/ PDF版本(版本号 2.48):http://pan. ...
- ABAP range 用法
转自http://www.sapjx.com/abap-range-table.html 1. Range Table 概述 Range Table 为 SAP R/3系统标准内表的一种,结构与 Se ...
- ulink函数的使用【学习笔记】
#include "apue.h" #include <fcntl.h> int main(void) { ) err_sys("open error&quo ...
- html5--6-55 动画效果-关键帧动画
html5--6-55 动画效果-关键帧动画 实例 @charset="UTF-8"; div{ width: 150px; height: 150px; font-size: 2 ...
- DataUtils
package com.cc.hkjc.util; import java.text.ParseException;import java.text.SimpleDateFormat;import j ...
- Codeforces-707D:Persistent Bookcase (离线处理特殊的可持久化问题&&Bitset)
Recently in school Alina has learned what are the persistent data structures: they are data structur ...
- Eclipse全项目搜索指定文件&字串
在eclipse中如果希望在大量的项目中寻找指定的文件可不是一件轻松的事,还好eclipse提供了强大的搜索功能. 我们可以通过通配符或正则表达式来设定查寻条件,下面是操作示例: ctrl+h 打开搜 ...