Helvetic Coding Contest 2016 online mirror F1
Description
Heidi has finally found the mythical Tree of Life – a legendary combinatorial structure which is said to contain a prophecy crucially needed to defeat the undead armies.
On the surface, the Tree of Life is just a regular undirected tree well-known from computer science. This means that it is a collection of npoints (called vertices), some of which are connected using n - 1 line segments (edges) so that each pair of vertices is connected by a path (a sequence of one or more edges).
To decipher the prophecy, Heidi needs to perform a number of steps. The first is counting the number of lifelines in the tree – these are paths of length 2, i.e., consisting of two edges. Help her!
The first line of the input contains a single integer n – the number of vertices in the tree (1 ≤ n ≤ 10000). The vertices are labeled with the numbers from 1 to n. Then n - 1 lines follow, each describing one edge using two space-separated numbers a b – the labels of the vertices connected by the edge (1 ≤ a < b ≤ n). It is guaranteed that the input represents a tree.
Print one integer – the number of lifelines in the tree.
4
1 2
1 3
1 4
3
5
1 2
2 3
3 4
3 5
4
In the second sample, there are four lifelines: paths between vertices 1 and 3, 2 and 4, 2 and 5, and 4 and 5.
我们可以dfs遍历,但只遍历两个点就return,每个点遍历一次,就是/2就好啦
#include<bits/stdc++.h>
using namespace std;
vector<int>q[100000];
int flag[100000];
int ans;
void dfs(int v,int sum)
{
if(sum==2)
{
ans++;
return;
}
if(flag[v])
{
return;
}
flag[v]=1;
for(int i=0;i<q[v].size();i++)
{
if(!flag[q[v][i]])
{
dfs(q[v][i],sum+1);
}
}
}
int main()
{
int n;
int v,u;
ans=0;
cin>>n;
for(int i=1;i<=n-1;i++)
{
cin>>v>>u;
q[v].push_back(u);
q[u].push_back(v);
}
for(int i=1;i<=n;i++)
{
memset(flag,0,sizeof(flag));
dfs(i,0);
}
cout<<ans/2<<endl;
return 0;
}
Helvetic Coding Contest 2016 online mirror F1的更多相关文章
- CF 690C3. Brain Network (hard) from Helvetic Coding Contest 2016 online mirror (teams, unrated)
题目描述 Brain Network (hard) 这个问题就是给出一个不断加边的树,保证每一次加边之后都只有一个连通块(每一次连的点都是之前出现过的),问每一次加边之后树的直径. 算法 每一次增加一 ...
- Helvetic Coding Contest 2016 online mirror A1
Description Tonight is brain dinner night and all zombies will gather together to scarf down some de ...
- Helvetic Coding Contest 2016 online mirror B1
Description The zombies are gathering in their secret lair! Heidi will strike hard to destroy them o ...
- Helvetic Coding Contest 2016 online mirror C2
Description Further research on zombie thought processes yielded interesting results. As we know fro ...
- Helvetic Coding Contest 2016 online mirror D1
Description "The zombies are lurking outside. Waiting. Moaning. And when they come..." &qu ...
- Helvetic Coding Contest 2016 online mirror C1
Description One particularly well-known fact about zombies is that they move and think terribly slow ...
- Helvetic Coding Contest 2019 online mirror (teams allowed, unrated)
http://codeforces.com/contest/1184 A1 找一对整数,使x^x+2xy+x+1=r 变换成一个分式,保证整除 #include<iostream> #in ...
- [Helvetic Coding Contest 2017 online mirror]
来自FallDream的博客,未经允许,请勿转载,谢谢, 第一次在cf上打acm...和同校大佬组队打 总共15题,比较鬼畜,最后勉强过了10题. AB一样的题目,不同数据范围,一起讲吧 你有一个背包 ...
- 【Codeforces】Helvetic Coding Contest 2017 online mirror比赛记
第一次打ACM赛制的团队赛,感觉还行: 好吧主要是切水题: 开场先挑着做五道EASY,他们分给我D题,woc什么玩意,还泊松分布,我连题都读不懂好吗! 果断弃掉了,换了M和J,然后切掉了,看N题: l ...
随机推荐
- jumpserver遇到的坑
安装:https://github.com/jumpserver/jumpserver,看readme照着做就行,下面是遇到的坑. 0.4.4版坑: 1.要升级pip,否则有的包装不上 2.p ...
- 测试你的浏览器是否支持WebGL(Does My Browser Support WebGL)
测试你的浏览器是否支持WebGL(Does My Browser Support WebGL) 关于WebGL:WebGL是一种3D绘图标准,这种绘图技术标准允许把JavaScript和OpenGL ...
- python中http请求中添加cookie支持
python3中构造http的Request需要用到urllib.request. 有时会用到cookie. 比如在访问网站首页得到cookie,通过下面代码添加cookie: #insta ...
- C#如何对DataTable中的数据进行条件搜索
经常遇到将数据库中的数据读取到DataTable中的时候再次对DataTable进行条件筛选,下面的筛选的一个例子: DataRow[] dr = dt.Select("token = '& ...
- 创建Azure Blob Snapshot的脚本
在前面的文章中介绍了如何创建Azure Blob Snapshot.那篇文章中创建的脚本思路是:遍历所有Storage Account,找到所有vhd文件,进行Snapshot的创建. 但这种方式不够 ...
- C++11中的原子操作(atomic operation)
所谓的原子操作,取的就是“原子是最小的.不可分割的最小个体”的意义,它表示在多个线程访问同一个全局资源的时候,能够确保所有其他的线程都不在同一时间内访问相同的资源.也就是他确保了在同一时刻只有唯一的线 ...
- js拼的onclick调用方法需要注意的地方 之二
那如果之前的方法不行,想传递json对象怎么办呢? 使用下面这种方法, getA = function(){ var obj = {'projectId':123,'projectName':'aac ...
- JQuery鼠标移动上去显示预览图
body中: <img src="../images/icon_view.gif" bigimg="../img.jpg" title="查看预 ...
- PCLVisualizer可视化类(4)
博客转载自:http://www.pclcn.org/study/shownews.php?lang=cn&id=168 多视口显示 所示,并进行比较分析,利用不同的搜索半径,基于同一点云计算 ...
- Metasploit和python两种安全工具的学习笔记
Metasploit是个好东西 主要参考了<Metasploit渗透测试魔鬼训练营>这本书. 一.先用自己的靶机感受一下该工具的强大 linux靶机的ip如图 按照书上写的配置,如图 然后 ...