• LightOJ 1300 Odd Personality


    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
     
    解题:大概意思就是说看有多少个起点,从这个起点出发,最后经过奇数条边后回到起点,但是不能经过相同的边,可以有相同的点。很明显在边双连通中找奇圈嘛。
     
     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 const int maxn = 50000;
     4 struct arc {
     5     int to,next;
     6     arc(int x = 0,int y = -1) {
     7         to = x;
     8         next = y;
     9     }
    10 } e[maxn<<2];
    11 int head[maxn],dfn[maxn],low[maxn],color[maxn];
    12 int tot,idx,cnt,n,m,flag,ans;
    13 bool b[maxn];
    14 void add(int u,int v) {
    15     e[tot] = arc(v,head[u]);
    16     head[u] = tot++;
    17 }
    18 void tarjan(int u,int fa) {
    19     dfn[u] = low[u] = ++idx;
    20     for(int i = head[u]; ~i; i = e[i].next) {
    21         if(!dfn[e[i].to]) {
    22             tarjan(e[i].to,u);
    23             low[u] = min(low[u],low[e[i].to]);
    24             if(low[e[i].to] > dfn[u]) b[i] = b[i^1] = true;
    25         } else if(e[i].to != fa) low[u] = min(low[u],dfn[e[i].to]);
    26     }
    27 }
    28 void dfs(int u,int s) {
    29     color[u] = s;
    30     cnt++;
    31     for(int i = head[u]; ~i; i = e[i].next) {
    32         if(b[i]) continue;
    33         if(color[e[i].to] && color[e[i].to] == color[u]) flag = 1;
    34         else if(color[e[i].to] == 0) dfs(e[i].to,3-s);
    35     }
    36 }
    37 int main() {
    38     int T,cs = 1,u,v;
    39     scanf("%d",&T);
    40     while(T--) {
    41         scanf("%d %d",&n,&m);
    42         memset(head,-1,sizeof(head));
    43         memset(low,0,sizeof(low));
    44         memset(dfn,0,sizeof(dfn));
    45         memset(color,0,sizeof(color));
    46         memset(b,false,sizeof(b));
    47         idx = 0;
    48         for(int i = tot = 0; i < m; ++i) {
    49             scanf("%d %d",&u,&v);
    50             add(u,v);
    51             add(v,u);
    52         }
    53         for(int i = ans = 0; i < n; ++i)
    54             if(!dfn[i]) tarjan(i,-1);
    55         for(int i = 0; i < n; ++i)
    56             if(color[i] == 0) {
    57                 flag = cnt = 0;
    58                 dfs(i,1);
    59                 if(flag) ans += cnt;
    60             }
    61         printf("Case %d: %d
    ",cs++,ans);
    62     }
    63     return 0;
    64 }
    View Code
  • 相关阅读:
    Jzoj2682 最长双回文串
    Jzoj2682 最长双回文串
    【hdu3853】Loops
    【tyvj1015】【caioj1060】公路乘车
    【luogu1064】金明的预算方案
    【bzoj1260】【CQOI2007】涂色paint
    【UVa1629】Cake slicing
    【NYOJ746】整数划分(四)
    【NYOJ 15】括号匹配2
    【poj2955】Brackets
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4394656.html
Copyright © 2020-2023  润新知