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. Git学习笔记 1,GitHub常用命令1

    廖雪峰Git教程 莫烦Git教程 莫烦Git视频教程 --------------- init > apt-get install git # 安装 > mkdir /home/yzn_g ...

  2. 紫书 例题 10-17 UVa 1639(数学期望+对数保存精度)

    设置最后打开的是盒子1, 另外一个盒子剩下i个 那么在这之前打开了n + n - i次盒子 那么这个时候的概率是C(2 * n - i, n) p ^ (n+1) (1-p)^ (n - i) 那么反 ...

  3. Dynamic Rankings(分块)

    P2617 Dynamic Rankings 题目描述 给定一个含有n个数的序列a[1],a[2],a[3]--a[n],程序必须回答这样的询问:对于给定的i,j,k,在a[i],a[i+1],a[i ...

  4. HDU 4939 Stupid Tower Defense 简单DP

    题意: 地图为长为n个单位长度的直线,每通过一个单位长度需要t秒. 有3种塔,红塔可以在当前格子每秒造成x点伤害,绿塔可以在之后格子造成y点伤害,蓝塔可以使通过单位长度的时间增加z秒. 让你安排塔的排 ...

  5. 洛谷 P1071 潜伏者

    P1071 潜伏者 题目描述 R 国和 S 国正陷入战火之中,双方都互派间谍,潜入对方内部,伺机行动.历尽艰险后,潜伏于 S 国的 R 国间谍小 C 终于摸清了 S 国军用密码的编码规则: 1. S ...

  6. WAS集群系列(5):集群搭建:步骤3:安装IHS软件

    选择"安装IBM HTTPServer"选项,点击"安装向导".例如以下图提示: 安装提示,逐步点击"下一步",当中偶有几处细节注意就可以. ...

  7. MFC Wizard创建的空应用程序中各个文件内容的解析

    创建的MFC应用程序名为:wd,那么: 一.wd.h解析 // wd.h : main header file for the WD application // #if !defined(AFX_W ...

  8. thinkphp里面的or查询

    thinkphp里面的or查询 whereOr 方法 使用whereOr 方法进行OR 查询: Db::table('think_user') ->where('name','like','%t ...

  9. Weka中数据挖掘与机器学习系列之Weka Package Manager安装所需WEKA的附加算法包出错问题解决方案总结(八)

    不多说,直接上干货! Weka中数据挖掘与机器学习系列之Weka系统安装(四) Weka中数据挖掘与机器学习系列之Weka3.7和3.9不同版本共存(七) 情况1 对于在Weka里,通过Weka  P ...

  10. HTTP 协议基础及发展历史

    一. 5层网络模型介绍 低三层 物理层:主要作用是定义物理设备如何传输数据. 数据链路层:在通信的实体间建立数据链路连接. 网路层:为数据在结点之间传输创建逻辑链路. 传输层: 想用户提供可靠的端到端 ...