HDU 4781/build a graph that meets the requirements

Source: Internet
Author: User

Ah, for the first time, we can see the number of points and the number of edges so that the directed graph can be restored as required.

Requirements:

1. Strong connectivity.

2. There is only one directed edge between any two points, and you and yourself are boundless.

3. Any closed loop right and % 3 are 0.

4. The weight theory of each edge is different, and the value is 1, 2, 3. M.


At the beginning, I thought there would be a big ring 1-> 2-> 3->... N, N-> 1. When I simulated the game, I didn't think about it anymore ..

Add edge first: I-> I + 1 is the right to I, and then select a right to add n to 1 from (n, n + 1, n + 2, make the closed loop % 3 = 0.

Then process the remaining M-n edges,

: Note that if an edge is appended to I ---> J, w (I, j) % 3 = sum of edge weights from I to J % 3 (easy to prove) exists)

Enumerate each entry (n <80, m <n * n/7 ).

#include<iostream>#include<cstring>using namespace std;int n,m;int mark[6400];int vis[85][85];   int main(){    int T;    cin>>T;int ct=1;    while(T--)    {        for(int i=0;i<=m;i++)            mark[i]=0;        memset(vis,0,sizeof(vis));           cin>>n>>m;        for(int i=1;i<n;i++)        {           // cout<<i<<" "<<i+1<<" "<<i<<endl;            mark[i]=1;vis[i][i+1]=vis[i+1][i]=i;        }        int tei=0;        for(int i=n;i<n+3;i++)           if((n*(n-1)/2+i)%3==0)           {              //cout<<n<<" "<<1<<" "<<i<<endl;              tei=i;              mark[i]=1;              vis[n][1]=vis[1][n]=i;              break;          }       for(int i=n;i<=m;i++)       {           if(!mark[i])           {               int flags=0;               for(int j=1;j<=n;j++)               {                for(int k=j+2;k<=n;k++)                {                    if(!vis[j][k]&&((k-j)*(j+k-1)/2)%3==i%3)                    {                        vis[j][k]=vis[k][j]=i;                        mark[i]=1;                        flags=1;                        break;                    }                }                if(flags)break;               }           }       }       bool flag=1;       for(int i=1;i<=m;i++)       {           if(mark[i]==0)             {                 flag=0;break;             }       }       cout<<"Case #"<<ct++<<":"<<endl;       if(flag==0)       cout<<-1<<endl;       else       {           for(int i=1;i<=n;i++)             for(int j=(i+1);j<=n;j++)             {                 if(vis[i][j]!=0&&!(i==1&&j==n))                 {                     cout<<i<<" "<<j<<" "<<vis[i][j]<<endl;                 }             }            cout<<n<<" "<<1<<" "<<tei<<endl;       }    }}




HDU 4781/build a graph that meets the requirements

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.