• Wizard's Tour


    F. Wizard's Tour
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    All Berland residents are waiting for an unprecedented tour of wizard in his Blue Helicopter over the cities of Berland!

    It is well-known that there are n cities in Berland, some pairs of which are connected by bidirectional roads. Each pair of cities is connected by no more than one road. It is not guaranteed that the road network is connected, i.e. it is possible that you can't reach some city from some other.

    The tour will contain several episodes. In each of the episodes:

    • the wizard will disembark at some city x from the Helicopter;
    • he will give a performance and show a movie for free at the city x;
    • he will drive to some neighboring city y using a road;
    • he will give a performance and show a movie for free at the city y;
    • he will drive to some neighboring to y city z;
    • he will give a performance and show a movie for free at the city z;
    • he will embark the Helicopter and fly away from the city z.

    It is known that the wizard doesn't like to use roads, so he agrees to use each road at most once (regardless of direction). In other words, for road between a and b he only can drive once from a to b, or drive once from b to a, or do not use this road at all.

    The wizards wants to plan as many episodes as possible without violation the above rules. Help the wizard!

    Please note that the wizard can visit the same city multiple times, the restriction is on roads only.

    Input

    The first line contains two integers n, m (1 ≤ n ≤ 2·105, 0 ≤ m ≤ 2·105) — the number of cities and the number of roads in Berland, respectively.

    The roads description follow, one in each line. Each description is a pair of two integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi), where ai and bi are the ids of the cities connected by the i-th road. It is guaranteed that there are no two roads connecting the same pair of cities. Every road is bidirectional. The cities are numbered from 1 to n.

    It is possible that the road network in Berland is not connected.

    Output

    In the first line print w — the maximum possible number of episodes. The next w lines should contain the episodes in format x, y, z — the three integers denoting the ids of the cities in the order of the wizard's visits.

    Examples
    Input
    4 5
    1 2
    3 2
    2 4
    3 4
    4 1
    Output
    2
    1 4 2
    4 3 2
    Input
    5 8
    5 3
    1 2
    4 5
    5 1
    2 5
    4 3
    1 4
    3 2
    Output
    4
    1 4 5
    2 3 4
    1 5 3
    5 2 1
    分析:给一个图,求最多能组成多少个V图形,其中每条边只能用一次;
       可以证明,对于每个联通块,最多可以组成edge/2个V图形;
       考虑递归处理;
       对于当前节点,标记所有没用的边,并把节点放入当前集合;
       递归处理集合中的节点,如果没有访问过,则递归该节点;
       如果递归返回一个节点,说明有未配对边,与当前边配对;
       否则,当前边未配对,在全部结束后两两配对即可;
       若配对后剩下一条边,返回到父亲即可;
    代码:
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <algorithm>
    #include <climits>
    #include <cstring>
    #include <string>
    #include <set>
    #include <bitset>
    #include <map>
    #include <queue>
    #include <stack>
    #include <vector>
    #include <cassert>
    #include <ctime>
    #define rep(i,m,n) for(i=m;i<=(int)n;i++)
    #define inf 0x3f3f3f3f
    #define mod 1000000007
    #define vi vector<int>
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define ll long long
    #define pi acos(-1.0)
    #define pii pair<int,int>
    #define sys system("pause")
    #define ls (rt<<1)
    #define rs (rt<<1|1)
    #define all(x) x.begin(),x.end()
    const int maxn=2e5+10;
    const int N=2e5+10;
    using namespace std;
    ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
    ll qmul(ll p,ll q,ll mo){ll f=0;while(q){if(q&1)f=(f+p)%mo;p=(p+p)%mo;q>>=1;}return f;}
    ll qpow(ll p,ll q,ll mo){ll f=1;while(q){if(q&1)f=f*p%mo;p=p*p%mo;q>>=1;}return f;}
    int n,m,k,t;
    map<ll,int>p,w;
    vi e[maxn];
    bool vis[maxn];
    struct node
    {
        int x,y,z;
    };
    vector<node>ret;
    bool ok(int x,int y,int z)
    {
        int ex=x,ey=y;
        if(ex>ey)swap(ex,ey);
        w[1LL*ex*N+ey]=1;
        ex=y,ey=z;
        if(ex>ey)swap(ex,ey);
        w[1LL*ex*N+ey]=1;
    }
    int dfs(int x)
    {
        int i;
        vis[x]=true;
        vi bl;
        rep(i,0,e[x].size()-1)
        {
            int y=e[x][i];
            int z=x;
            if(y>z)swap(y,z);
            if(!p.count(1LL*y*N+z))
            {
                bl.pb(e[x][i]),
                p[1LL*y*N+z]=1;
            }
        }
        rep(i,0,bl.size()-1)
        {
            int y=bl[i];
            if(vis[y])continue;
            int z=dfs(y);
            if(z)ret.pb(node{x,y,z}),ok(x,y,z);
        }
        int y=0,z=0;
        rep(i,0,bl.size()-1)
        {
            z=bl[i];
            if(!w.count(1LL*min(z,x)*N+max(z,x)))
            {
                if(y)
                {
                    ret.pb(node{y,x,z});
                    ok(y,x,z);
                    y=z=0;
                }
                else y=z,z=0;
            }
        }
        return y;
    }
    int main(){
        int i,j;
        scanf("%d%d",&n,&m);
        rep(i,1,m)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            e[x].pb(y),e[y].pb(x);
        }
        rep(i,1,n)if(!vis[i])dfs(i);
        printf("%d
    ",ret.size());
        rep(i,0,ret.size()-1)
        {
            printf("%d %d %d
    ",ret[i].x,ret[i].y,ret[i].z);
        }
        return 0;
    }
  • 相关阅读:
    理解AJAX工作原理 构建网站的有效方法()
    吃大亏的SQL
    Global
    jQuery插件jTemplates(模板
    distinct吃亏记
    事件和委托的编译代码
    星期划分
    理解AJAX工作原理 构建网站的有效方法(5)
    理解AJAX工作原理 构建网站的有效方法(2)
    我比较笨系列2.《委托》将方法绑定到委托
  • 原文地址:https://www.cnblogs.com/dyzll/p/7551803.html
Copyright © 2020-2023  润新知