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 ...
随机推荐
- PLSQL查询最近编绎、创建、修改过的过程函数
SELECT * FROM User_Objects t WHERE t.Object_Type IN ('PROCEDURE', 'PACKAGE BODY', 'FUNCTION') AND t. ...
- bzoj 3307: 雨天的尾巴 线段树合并
题目大意: N个点,形成一个树状结构.有M次发放,每次选择两个点x,y对于x到y的路径上(含x,y)每个点发一袋Z类型的物品.问完成所有发放后,每个点存放最多的是哪种物品. 题解: 首先我们为每一个节 ...
- "_OBJC_CLASS_$_ALAssetsLibrary", referenced from:和clang: error: linker command failed with exit code 1 (use -v to see invocation)错误
在项目中使用MWPhotoBrowser未导入ALAssetsLibrary类库时会导致编译时出现异常: "_OBJC_CLASS_$_ALAssetsLibrary", refe ...
- easy_install下载地址及安装
下载地址 https://pypi.python.org/pypi/setuptools 解压 tar -xzvf xx.tar.gz 安装 cd 解压目录 sudo python setup.py ...
- 微服务理论之三:RPC框架原理
RPC调用是面向服务架构场景下进行服务间调用的常用组件,一个完整的RPC调用的流程如图1所示: 图1 RPC调用流程 为了方便RPC调用者和服务者的开发,开发者们开发了很多RPC框架.比较有名的RPC ...
- 三 Flask web开发快速入门
1:会话: from flask import Flask, url_for, request, render_template, session from werkzeug.utils import ...
- Spring MVC配置详解(3)
一.Spring MVC环境搭建:(Spring 2.5.6 + Hibernate 3.2.0) 1. jar包引入 Spring 2.5.6:spring.jar.spring-webmvc.ja ...
- C#设计模式(8)——桥接模式
一.概念 桥接模式即将抽象部分与实现部分脱耦,使它们可以独立变化. 二.模型 三.代码实现 // 客户端调用 // 类似Web应用程序 class Client { static void Main( ...
- [java] volatile关键字对while循环条件提升问题补充
在java并发编程中,代码如下: volatile boolean asleep; ... while(!asleep){ countSomeSheep(); } 如果此处忘记将asleep变量设置为 ...
- 网络编程之socket编程
TCP/IP协议 网络层的“ip地址”可以唯一标识网络中的主机,而传输层的“协议+端口”可以唯一标识主机中的应用程序(进程).这样利用三元组(ip地址,协议,端口)就可以标识网络的进程了,网络中的进程 ...