HDOJ--4781 -- assignment for Princess [structure directed graph]

Source: Internet
Author: User

Link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4781

Question: Here are two numbers. N represents the number of vertices and m represents the number of edges. To create a graph, you must:

1. Directed Graph with at most one edge between two points.

2. The Edge Weight is 1 ~ M. Each value can be used only once.

3. Any point can reach the other vertices.

4. The sum of edge weights of any ring is a multiple of 3.

5. There is no own ring.


Idea: first from 1 vertex to N, two adjacent vertices construct a directed edge with the weights of 1, 2, 3 ...... N-1, N to 1 are connected to an edge, n, n + 1, or N + 2, and the value that satisfies the weight and is a multiple of 3, in this way, the n vertices constitute a large ring and meet the weights and are multiples of 3. Because the minimum m value is N + 3, this large ring can be constructed.

For the remaining M-n edges, assume that a directed edge with the weight of X is connected to the vertices u and v, the edge values of X % 3 and U and V are equal to sum % 3, as shown in

Vertex {1, 2, 3, 4} is a ring. The edge weight is a multiple of 3. vertex {1, 2, 4} is also a ring. The Edge Weight is also a multiple of 3, because edge [2] [4] replaces edge [2] [3] + edge [3] [4], only when their weight module3 is equal can the weights of the new ring be equal to the multiples of 3.

Since we construct a large ring from a small to a large structure, when adding the remaining M-n edges, the edge should also be a point with a small vertex subscript from a point with a large vertex subscript, because this is a directed graph, the ring is correct only in this way. If the edges between vertex 2 and vertex 4 in the graph are from 4 to 2, although (edge [2] [4] % 3) = (edge [2] [3] + edge [3] [4]) % 3, but at this time {2, 3, 4} forms a ring, obviously, the edge weight is not a multiple of 3.

However, after my tests, the data on this question is not rigorous. Even if the direction is not smaller than the vertex subscript to the vertex subscript, the AC


#include<cstring>#include<string>#include<fstream>#include<iostream>#include<iomanip>#include<cstdio>#include<cctype>#include<algorithm>#include<queue>#include<map>#include<set>#include<vector>#include<stack>#include<ctime>#include<cstdlib>#include<functional>#include<cmath>using namespace std;#define PI acos(-1.0)#define MAXN 110#define eps 1e-7#define INF 0x7FFFFFFF#define seed 131#define ll long long#define ull unsigned long long#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1struct node{    int u,v,dis;}edge[10000];int vis[10000],mapp[90][90],sum[90];int n,m,cnt,flag;void gao(int x){    int i,j;    for(i=1;i<=n;i++){        for(j=i+1;j<=n;j++){            if(!mapp[i][j]&&!mapp[j][i]){                if((sum[j]-sum[i]+3)%3==x%3){                    edge[cnt].u = i;                    edge[cnt].v = j;                    edge[cnt].dis = x;                    cnt++;                    mapp[i][j] = 1;                    vis[x] = 1;                    return ;                }            }        }    }    flag = 1;}int main(){    int t,i,j,k=1;    scanf("%d",&t);    while(t--){        memset(mapp,0,sizeof(mapp));        memset(sum,0,sizeof(sum));        memset(vis,0,sizeof(vis));        cnt = 0;        flag = 0;        int tot = 0;        scanf("%d%d",&n,&m);        for(i=1;i<n;i++){            tot += i;            edge[cnt].u = i;            edge[cnt].v = i+1;            edge[cnt].dis = i;            vis[i] = 1;            mapp[i][i+1] = 1;            cnt++;            sum[i] = (sum[i-1] + i - 1) % 3;        }        edge[cnt].u = n;        edge[cnt].v = 1;        if((tot+n)%3==0)    tot = n;        else if((tot+n+1)%3==0) tot = n + 1;        else    tot = n + 2;        edge[cnt].dis = tot;        vis[tot] = 1;        mapp[n][1] = 1;        cnt++;        sum[n] = (sum[n-1] + n - 1) % 3;        for(i=1;i<=m;i++){            if(!vis[i]){                gao(i);            }            if(flag)    break;        }        printf("Case #%d:\n",k++);        if(flag){            puts("-1");            continue;        }        for(i=0;i<m;i++){            printf("%d %d %d\n",edge[i].u,edge[i].v,edge[i].dis);        }    }    return 0;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.