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的更多相关文章

  1. 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 ...

  2. codeforces285C

    Building Permutation CodeForces - 285C Permutation p is an ordered set of integers p1,  p2,  ...,  p ...

  3. 多校联训 DS 专题

    CF1039D You Are Given a Tree 容易发现,当 \(k\) 不断增大时,答案不断减小,且 \(k\) 的答案不超过 \(\lfloor\frac {n}{k}\rfloor\) ...

  4. 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 ...

  5. [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 ...

  6. 图论/位运算 Codeforces Round #285 (Div. 2) C. Misha and Forest

    题目传送门 /* 题意:给出无向无环图,每一个点的度数和相邻点的异或和(a^b^c^....) 图论/位运算:其实这题很简单.类似拓扑排序,先把度数为1的先入对,每一次少一个度数 关键在于更新异或和, ...

  7. 水题 Codeforces Round #285 (Div. 2) C. Misha and Forest

    题目传送门 /* 题意:给出无向无环图,每一个点的度数和相邻点的异或和(a^b^c^....) 图论/位运算:其实这题很简单.类似拓扑排序,先把度数为1的先入对,每一次少一个度数 关键在于更新异或和, ...

  8. 【codeforces 755C】PolandBall and Forest

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  9. codeforces 501C. Misha and Forest 解题报告

    题目链接:http://codeforces.com/problemset/problem/501/C 题目意思:有 n 个点,编号为 0 - n-1.给出 n 个点的度数(即有多少个点跟它有边相连) ...

随机推荐

  1. HDOJ_ How can I read input data until the end of file ?

    Language C C++ Pascal To read numbers int n;while(scanf("%d", &n) != EOF){ ...} int n; ...

  2. mybatis xml文件解析

    1 parameterType 如果参数只有一个,比如一个id,即int类型的id,那么parameterType直接是int. 如果参数有多个,那么就用表中一行对应的类,默认是类的名字和表中列的名字 ...

  3. inheritance super overrides printMethod in Superclass override重写父方法

    Core Java Web Page http://horstmann.com/corejava.html [ inheritance ] package v1ch05.inheritance; im ...

  4. Android 的assets文件资源与raw文件资源读取

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/zpf8861/article/details/34504183   res/raw和assets的同 ...

  5. rails elasticsearch searchkick用法

    1.安装elasticsearch 之前要先安装java8: 参考https://www.elastic.co/guide/en/elasticsearch/reference/current/zip ...

  6. Hive Metastore

    metastore:实际保存表信息的地方.     包括: 数据库,表的基本信息:权限信息:存储格式信息:                 各种属性信息:                 权限信息: ...

  7. date format记录

    各种日期格式定义,容易忘记,这里备注下: * 支持格式为 yyyy.MM.dd G 'at' hh:mm:ss z 如 '2002-1-1 AD at 22:10:59 PSD'<br> ...

  8. AJAX 用户验证方法

    JSP <td width="10%" class="main_matter_td">真实姓名</td> <td width=&q ...

  9. java中wait和notify

    在JAVA中,是没有类似于PV操作.进程互斥等相关的方法的.JAVA的进程同步是通过synchronized()来实现的,需要说明的是,JAVA的synchronized()方法类似于操作系统概念中的 ...

  10. ASP.NET WebServce项目下添加Http服务,支持Get,Post请求方式;传输格式json/xml

    由于WEBServce老项目中需要增添新的接口,而且添加的接口不希望被其它项目以引用Servces方式使用. 那么得在现有Service项目中添加Http请求方式来实现系统间数据交互.只需要告知请求地 ...