zoukankan      html  css  js  c++  java
  • Codeforces Round #192 (Div. 1) C. Graph Reconstruction 随机化

    C. Graph Reconstruction

    Time Limit: 20 Sec

    Memory Limit: 256 MB

    题目连接

    http://codeforces.com/contest/329/problem/C

    Description

    I have an undirected graph consisting of n nodes, numbered 1 through n. Each node has at most two incident edges. For each pair of nodes, there is at most an edge connecting them. No edge connects a node to itself.

    I would like to create a new graph in such a way that:

    • The new graph consists of the same number of nodes and edges as the old graph.
    • The properties in the first paragraph still hold.
    • For each two nodes u and v, if there is an edge connecting them in the old graph, there is no edge connecting them in the new graph.

    Help me construct the new graph, or tell me if it is impossible.


    Under two situations the player could score one point.

    ⋅1. If you touch a buoy before your opponent, you will get one point. For example if your opponent touch the buoy #2 before you after start, he will score one point. So when you touch the buoy #2, you won't get any point. Meanwhile, you cannot touch buoy #3 or any other buoys before touching the buoy #2.

    ⋅2. Ignoring the buoys and relying on dogfighting to get point. If you and your opponent meet in the same position, you can try to fight with your opponent to score one point. For the proposal of game balance, two players are not allowed to fight before buoy #2 is touched by anybody.

    There are three types of players.

    Speeder: As a player specializing in high speed movement, he/she tries to avoid dogfighting while attempting to gain points by touching buoys.
    Fighter: As a player specializing in dogfighting, he/she always tries to fight with the opponent to score points. Since a fighter is slower than a speeder, it's difficult for him/her to score points by touching buoys when the opponent is a speeder.
    All-Rounder: A balanced player between Fighter and Speeder.

    There will be a training match between Asuka (All-Rounder) and Shion (Speeder).
    Since the match is only a training match, the rules are simplified: the game will end after the buoy #1 is touched by anybody. Shion is a speed lover, and his strategy is very simple: touch buoy #2,#3,#4,#1 along the shortest path.

    Asuka is good at dogfighting, so she will always score one point by dogfighting with Shion, and the opponent will be stunned for T seconds after dogfighting. Since Asuka is slower than Shion, she decides to fight with Shion for only one time during the match. It is also assumed that if Asuka and Shion touch the buoy in the same time, the point will be given to Asuka and Asuka could also fight with Shion at the buoy. We assume that in such scenario, the dogfighting must happen after the buoy is touched by Asuka or Shion.

    The speed of Asuka is V1 m/s. The speed of Shion is V2 m/s. Is there any possibility for Asuka to win the match (to have higher score)?

    Input

    The first line consists of two space-separated integers: n and m (1 ≤ m ≤ n ≤ 105), denoting the number of nodes and edges, respectively. Then m lines follow. Each of the m lines consists of two space-separated integers u and v (1 ≤ u, v ≤ nu ≠ v), denoting an edge between nodes u and v.

    Output

    If it is not possible to construct a new graph with the mentioned properties, output a single line consisting of -1. Otherwise, output exactly m lines. Each line should contain a description of edge in the same way as used in the input format.

    Sample Input

    8 7
    1 2
    2 3
    4 5
    5 6
    6 8
    8 7
    7 4

    Sample Output

    1 4
    4 6
    1 6
    2 7
    7 5
    8 5
    2 8

    HINT

    题意

    给你一个n个点m条边的无向图

    无自环,无重边,每个点的度数最多为2

    然后让你找到一个图,使得性质一样,但是之前相连的边,之后不能相连

    题解:

    随机化,首先满足题意的图应该有很多个,所以瞎随……

    注意随机的时候,用一种保证每个点的度数最多为2的方式随机就好了

    代码

    #include<iostream>
    #include<stdio.h>
    #include<map>
    #include<vector>
    #include<algorithm>
    #include<ctime>
    using namespace std;
    
    map<pair<int,int> ,int>H;
    int main()
    {
        srand(time(NULL));
        int n,m;
        scanf("%d%d",&n,&m);
        vector<int> Q;
        for(int i=1;i<=n;i++)
            Q.push_back(i);
        for(int i=1;i<=m;i++)
        {
            int x,y;scanf("%d%d",&x,&y);
            if(x>y)swap(x,y);
            H[make_pair(x,y)]=1;
        }
        int tot = 0;
        while(1)
        {
            tot++;
            random_shuffle(Q.begin(),Q.end());
            int flag = 0;
            for(int i=0;i<m;i++)
            {
                int k = Q[i],p = Q[(i+1)%n];
                if(k==p)
                {
                    flag=1;
                    break;
                }
                if(k>p)
                    swap(k,p);
                if(H[make_pair(k,p)])
                {
                    flag=1;
                    break;
                }
            }
            if(flag==0)
                break;
            if(tot==100)
            {
                printf("-1
    ");
                return 0;
            }
        }
        for(int i=0;i<m;i++)
            printf("%d %d
    ",Q[(i+1)%n],Q[i]);
    
    }
  • 相关阅读:
    javascript中构造函数与普通函数的区别还有关于“new”操作符的一些原理
    一个简单的jquery左右列表内容切换应用
    Spring学习笔记(三)之装配Bean
    Maven引入jar的总结
    在使用hibernate的getHibernateTemplate()时怎么让控制台输出封装好的SQL? 怎么用日志打印出来?
    Spring学习笔记(二)之装配Bean
    spring学习笔记(一) Spring概述
    ResourceBundle读取文件学习
    如何从结果集中遍历得到一条条的数据?
    response与request回顾学习
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4935620.html
Copyright © 2011-2022 走看看