LightOJ 1300 Odd Personality
Odd Personality
This problem will be judged on LightOJ. Original ID: 1300
64-bit integer IO format: %lld Java class name: Main
Being an odd person, Jim always liked odd numbers. One day he was visiting his home town which consists of n places and m bidirectional roads. A road always connects two different places and there is at most one road between two places. The places are numbered from 0 to n-1.
Jim wants to find a tour which starts from a place p and each time it goes to a new road and finally at last step it returns back to p. As Jim likes odd numbers, he wants the length of the tour to be odd. And the length of a tour is defined by the number of roads used in the tour.
For the city map given above, 0 - 1 - 2 - 0 is such a tour, so, 0 is one of the results, since from 0, a tour of odd length is found, similarly, 1 - 2 - 0 - 1 is also a valid tour. But 3 - 2 - 0 - 1 - 2 - 3 is not. Since the road 2 - 3 is used twice. Now given the city map, Jim wants to find the number of places where he can start his journey for such a tour. As you are the best programmer in town, he asks you for help. Jim can use a place more than once, but a road can be visited at most once in the tour.
Input
Input starts with an integer T (≤ 30), denoting the number of test cases.
Each case starts with a blank line. The next line contains two integers: n (3 ≤ n ≤ 10000) and m (0 ≤ m ≤ 20000). Each of the next m lines contains two integers u v (0 ≤ u, v < n, u ≠ v) meaning that there is a bidirectional road between place u and v. The input follows the above constraints. And no road is reported more than once.
Output
For each case, print the case number and the total number places where Jim can start his journey.
Sample Input
1
6 6
0 1
1 2
2 0
3 2
3 4
3 5
Sample Output
Case 1: 3
Source
#include <bits/stdc++.h>
using namespace std;
const int maxn = ;
struct arc {
int to,next;
arc(int x = ,int y = -) {
to = x;
next = y;
}
} e[maxn<<];
int head[maxn],dfn[maxn],low[maxn],color[maxn];
int tot,idx,cnt,n,m,flag,ans;
bool b[maxn];
void add(int u,int v) {
e[tot] = arc(v,head[u]);
head[u] = tot++;
}
void tarjan(int u,int fa) {
dfn[u] = low[u] = ++idx;
for(int i = head[u]; ~i; i = e[i].next) {
if(!dfn[e[i].to]) {
tarjan(e[i].to,u);
low[u] = min(low[u],low[e[i].to]);
if(low[e[i].to] > dfn[u]) b[i] = b[i^] = true;
} else if(e[i].to != fa) low[u] = min(low[u],dfn[e[i].to]);
}
}
void dfs(int u,int s) {
color[u] = s;
cnt++;
for(int i = head[u]; ~i; i = e[i].next) {
if(b[i]) continue;
if(color[e[i].to] && color[e[i].to] == color[u]) flag = ;
else if(color[e[i].to] == ) dfs(e[i].to,-s);
}
}
int main() {
int T,cs = ,u,v;
scanf("%d",&T);
while(T--) {
scanf("%d %d",&n,&m);
memset(head,-,sizeof(head));
memset(low,,sizeof(low));
memset(dfn,,sizeof(dfn));
memset(color,,sizeof(color));
memset(b,false,sizeof(b));
idx = ;
for(int i = tot = ; i < m; ++i) {
scanf("%d %d",&u,&v);
add(u,v);
add(v,u);
}
for(int i = ans = ; i < n; ++i)
if(!dfn[i]) tarjan(i,-);
for(int i = ; i < n; ++i)
if(color[i] == ) {
flag = cnt = ;
dfs(i,);
if(flag) ans += cnt;
}
printf("Case %d: %d\n",cs++,ans);
}
return ;
}
LightOJ 1300 Odd Personality的更多相关文章
- lightoj 1300 边双联通分量+交叉染色求奇圈
题目链接:http://lightoj.com/volume_showproblem.php?problem=1300 边双连通分量首先dfs找出桥并标记,然后dfs交叉着色找奇圈上的点.这题只要求在 ...
- POJ 1300 Door Man(欧拉回路的判定)
题目链接 题意 : 庄园有很多房间,编号从0到n-1,能否找到一条路径经过所有开着的门,并且使得通过门之后就把门关上,关上的再也不打开,最后能回到编号为0的房间. 思路 : 这就是一个赤裸裸的判断欧拉 ...
- LightOJ 13361336 - Sigma Function (找规律 + 唯一分解定理)
http://lightoj.com/volume_showproblem.php?problem=1336 Sigma Function Time Limit:2000MS Memory L ...
- POJ 1300.Door Man 欧拉通路
Door Man Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2596 Accepted: 1046 Descript ...
- poj 1300 欧拉图
http://poj.org/problem?id=1300 要不是书上有翻译我估计要卡死,,,首先这是一个连通图,鬼知道是那句话表示出来的,终点必须是0,统计一下每个点的度数,如果是欧拉回路那么起点 ...
- Sigma Function (LightOJ - 1336)【简单数论】【算术基本定理】【思维】
Sigma Function (LightOJ - 1336)[简单数论][算术基本定理][思维] 标签: 入门讲座题解 数论 题目描述 Sigma function is an interestin ...
- [LeetCode] Odd Even Linked List 奇偶链表
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...
- 区间DP LightOJ 1422 Halloween Costumes
http://lightoj.com/volume_showproblem.php?problem=1422 做的第一道区间DP的题目,试水. 参考解题报告: http://www.cnblogs.c ...
- Odd Even Linked List
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...
随机推荐
- docker切换默认镜像源
docker切换默认镜像源 基于 debian8 默认安装的 docker 镜像源是在国外,pull 镜像的时候奇慢无比,需要自己手动切换成国内的镜像源. 1. 修改配置文件 docker 默认的 ...
- 洛谷3833 [SHOI2012]魔法树
SHOI2012 D2T3 题目描述 Harry Potter 新学了一种魔法:可以让改变树上的果子个数.满心欢喜的他找到了一个巨大的果树,来试验他的新法术. 这棵果树共有N个节点,其中节点0是根节点 ...
- PatentTips - Compare and exchange operation using sleep-wakeup mechanism
BACKGROUND Typically, a multithreaded processor or a multi-processor system is capable of processing ...
- Eclipse快捷键 10个最实用的快捷键
Eclipse中10个最实用的快捷键组合 一个Eclipse骨灰级开发人员总结了他觉得最实用但又不太为人所知的快捷键组合.通过这些组合能够更加easy的浏览源代码.使得总体的开发效率和质量得到提升. ...
- 基于express+redis高速实现实时在线用户数统计
作者:zhanhailiang 日期:2014-11-09 本文将介绍怎样基于express+redis高速实现实时在线用户数统计. 1. 在github.com上创建项目uv-tj.将其同步到本地: ...
- PHP长整型在32位系统中强制转化溢出
CleverCode近期遇到一个PHP项目整形转化问题,mysql有一个字段id是bigint的,里面有长整型,如id = 5147486396.可是php代码因为历史原因却部署在多台机器中,当中A机 ...
- jfinal 后台文件上传(结合上一篇(h5 图片回显))
前端用了jquery.form.js插件异步提交表单 $("#imgForm").ajaxSubmit();//户主头像 /** * * @description 上传户主头像 * ...
- HDU 5883 欧拉回路
题面: 思路: 这里面有坑啊啊啊-.. 先普及一下姿势: 判断无向图欧拉路的方法: 图连通,只有两个顶点是奇数度,其余都是偶数度的. 判断无向图欧拉回路的方法: 图连通,所有顶点都是偶数度. 重点:图 ...
- Android App中使用Gallery制作幻灯片播放效果
http://www.jb51.net/article/83313.htm 我们有时候在iPhone手机上或者Windows上面看到动态的图片,可以通过鼠标或者手指触摸来移动它,产生动态的图片滚动效果 ...
- Android真机调试访问本地服务器(localhost)的解决方案
Android系统把它自己作为了localhost!当连接localhost都是他自己啊.. 囧,在这里晕了好久才发现.. 网上介绍的都是模拟器连接本地服务器的,我试着把链接改为http://10.0 ...