Odd Personality

Time Limit: 2000ms
Memory Limit: 32768KB

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

Problem Setter: Jane Alam Jan
 
解题:大概意思就是说看有多少个起点,从这个起点出发,最后经过奇数条边后回到起点,但是不能经过相同的边,可以有相同的点。很明显在边双连通中找奇圈嘛。
 
 #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的更多相关文章

  1. lightoj 1300 边双联通分量+交叉染色求奇圈

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1300 边双连通分量首先dfs找出桥并标记,然后dfs交叉着色找奇圈上的点.这题只要求在 ...

  2. POJ 1300 Door Man(欧拉回路的判定)

    题目链接 题意 : 庄园有很多房间,编号从0到n-1,能否找到一条路径经过所有开着的门,并且使得通过门之后就把门关上,关上的再也不打开,最后能回到编号为0的房间. 思路 : 这就是一个赤裸裸的判断欧拉 ...

  3. LightOJ 13361336 - Sigma Function (找规律 + 唯一分解定理)

    http://lightoj.com/volume_showproblem.php?problem=1336 Sigma Function Time Limit:2000MS     Memory L ...

  4. POJ 1300.Door Man 欧拉通路

    Door Man Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2596   Accepted: 1046 Descript ...

  5. poj 1300 欧拉图

    http://poj.org/problem?id=1300 要不是书上有翻译我估计要卡死,,,首先这是一个连通图,鬼知道是那句话表示出来的,终点必须是0,统计一下每个点的度数,如果是欧拉回路那么起点 ...

  6. Sigma Function (LightOJ - 1336)【简单数论】【算术基本定理】【思维】

    Sigma Function (LightOJ - 1336)[简单数论][算术基本定理][思维] 标签: 入门讲座题解 数论 题目描述 Sigma function is an interestin ...

  7. [LeetCode] Odd Even Linked List 奇偶链表

    Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...

  8. 区间DP LightOJ 1422 Halloween Costumes

    http://lightoj.com/volume_showproblem.php?problem=1422 做的第一道区间DP的题目,试水. 参考解题报告: http://www.cnblogs.c ...

  9. Odd Even Linked List

    Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...

随机推荐

  1. python-排序算法 冒泡和快速排序

    交换排序 交换排序有冒泡排序和快速排序 冒泡排序 冒泡排序就是每次找出最大(最小)元素,放在集合最前或最后,这是最简单的排序算法 print("未排序之前:",collection ...

  2. IDEA修改当前工程jdk版本

    1.ctrl+shift+alt+s 2.根据实际情况修改jdk版本

  3. fork函数详解

    一.fork入门知识 一个进程,包括代码.数据和分配给进程的资源.fork()函数通过系统调用创建一个与原来进程几乎完全相同的进程,也就是两个进程可以做完全相同的事,但如果初始参数或者传入的变量不同, ...

  4. 05001_Linux简介

    1.Linux的概述 (1)Linux是基于Unix的开源免费的操作系统,由于系统的稳定性和安全性几乎成为程序代码运行的最佳系统环境.Linux是由Linus Torvalds(林纳斯•托瓦兹)起初开 ...

  5. ArcGIS api for javascript——图层-创建WMS图层类型的图层

    本例使用一个WMS端点创建了一个简单的动态图层.首先,代码声明一个新的类my.CityStatesRiversUSAWMSLayer,该类继承esri.layers.DynamicMapService ...

  6. Attach、Detach和DeleteObject

    原文:Attach.Detach和DeleteObject,想飞的梦想 1.CWnd Attatch和Detach的关系 首先,要明白Windows对象和MFC对象的区别. MFC对象实际上并没有把整 ...

  7. 用户向导左右滑动页面实现之ImageSwitcher

    当第一次打开一个app时,通常有一个使用向导介绍本APK的基本功能和用法,这个向导是很重要的,方便用户能高速知道和适应该app如何用. 实现此使用向导有非常多种方法,比方用ImageSwitcher, ...

  8. rac_grid自检提示缺少pdksh-5.2包

    原创作品,出自 "深蓝的blog" 博客,欢迎转载,转载时请务必注明下面出处,否则追究版权法律责任. 深蓝的blog:http://blog.csdn.net/huangyanlo ...

  9. 27.AngularJS 下载地址

    转自:https://www.cnblogs.com/best/tag/Angular/ 各个 angular.js 版本下载: https://github.com/angular/angular. ...

  10. org.w3c.dom.Document 与org.dom4j.Document互转

    public static Document parse(org.w3c.dom.Document doc) throws Exception { if (doc == null) { return ...