Question

A tennis player N players have plan to match each other total M pairs, one match per day and no two tennis player match more than one During that time, someone want to organize a tug of war battle,by devide a tennis player into two team, but to unite playing tug of war in each team must have player who never do tennis competition before and this is more important than number of player in both team Write a program that receive a Competition Schedule then calculate day at the lastest that still can divide a tennis player for playing tug of war after tennis competition Input First line specify 2 integer N and M (2 N< 100,000; 1< M<-100,000) After that M line will spcify competition schedule ie in line 1+I will specify 2 integer A and B for telling that in day 1 Platyer A will match with player B (1<-A?N; 1?B<=N) and garuntee that any pair of player wont play more than 1 day Output 1 line represent day that can divide tennnis player into two team for playing tug of war, player in the same team will never competition tennis before Example1 Input Output 12 2 3 3 1 Example2 Input 5 7 12 2 3 Output 6 5 2 5 1

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<bits/stdc++.h>
#define lli long long int
#define pb push_back
#define INF 1e18
#define N 100010


using namespace std;


/*

Main idea behind my implementation is as follows:

We can't have tug of war if we can't find two set of players such that no one has played againest each other
before

To have this , we have a specific algorithm called bipartiteness
If the graph obtained till the match on that day don't violate then we add that day

Once we have found it violates bipartiteness on a day , it will violate one the next days as well.

bipartiteness can be checked by performing a dfs which has a time complexity of O(V+E)


*/

vector<lli> adj[N];
bool visited[N];
lli color[N];

//dfs for checking bipartiteness
bool dfs(lli s,lli p)
{
   visited[s] = 1;
   lli i,v;
   color[s] = 1-color[p];//alternate color in neighbours
   for(i=0;i<adj[s].size();i++)
   {
       v = adj[s][i];
       if(!visited[v])
       {
           if(!dfs(v,s))
               return 0;
       }
       else
       {
           if(color[v] == color[s])//if two neighbours have same color then bipartiteness is violated , hence return false
               return 0;
       }
   }
   return 1;//return true if till now bipartiteness is followed
}

int main()
{
   lli n,m,i,j,k,u,v,ans;
   bool valid = 1;//to check if tog of war is possible
   cin>>n>>m;
  
  
   ans = 0;
   for(i=1;i<=m;i++)
   {
       cin>>u>>v;
       adj[u].pb(v); adj[v].pb(u);
      
       //if already violated then no pointing in checking further
       if(!valid)
           continue;
      
       memset(visited,0,sizeof visited);
       color[0] = 0;//0 is an aribrary node which is virtually taken as parent of roots for performing dfs
      
       for(j=1;j<=n;j++)
       {
           if(visited[j])
               continue;
           valid = dfs(j,0);
           if(!valid)
               break;
       }
       if(valid)
           ans++;
   }
  
  
   cout<<ans<<endl;
  
}

CA File Edit Search View Project Execute Tools AStyle WindowHelp PROSisth.cpp-Executing)- Dev-C+5.11 sth.cpp #include< bits/sCA File Edit Search View Project Execute Tools AStyle WindowHelp PROSisth.cpp-Executing)- Dev-C+5.11 sth.cpp #include< bits/s

Add a comment
Know the answer?
Add Answer to:
A tennis player N players have plan to match each other total M pairs, one match...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hey, i'm super stuck on my lab in cs102. I have attempted it but never got...

    Hey, i'm super stuck on my lab in cs102. I have attempted it but never got close to what the end result should be. Please help, due in 2 days!! Assignment You will be writing a program to simulate a slot machine. The player will start with $1000 and you will repeatedly ask the user how much money they wish to insert into the machine before spinning, or allow the player to quit. If the player runs out of money,...

  • Use C++ 11 to write the program War Game Requirement Setting This is a 2-player game....

    Use C++ 11 to write the program War Game Requirement Setting This is a 2-player game. It is played through dice. Rule for scoring The player who rolls higher number gets one point. If both players roll the same number, it is considered a draw and no one gets a point. Dice Specification There are two kinds of dice: normal die, represented by Die class. loaded die, represented by the loadedDie class. Classes Die class Die class has a member...

  • Topic:Data structures and algorithms programming Compiler:C++ 11, flag: -static -std=c++0x Boring job One day, Rick got...

    Topic:Data structures and algorithms programming Compiler:C++ 11, flag: -static -std=c++0x Boring job One day, Rick got a sequence of N magic numbers. In order to find the latent code in the sequence, he decides to do the following operations with a magic parameter K: Step (1) Take out the first k numbers from current sequences into a temporary sequence ni namn, and pick the largest element Vmax out of these k numbers, then subtract 1 from the remaining K-1 elements....

  • The following are screen grabs of the provided files Thanks so much for your help, and have a n...

    The following are screen grabs of the provided files Thanks so much for your help, and have a nice day! My Java Programming Teacher Gave me this for practice before the exam, butI can't get it to work, and I need a working version to discuss with my teacher ASAP, and I would like to sleep at some point before the exam. Please Help TEST QUESTION 5: Tamagotchi For this question, you will write a number of classes that you...

  • You need not run Python programs on a computer in solving the following problems. Place your...

    You need not run Python programs on a computer in solving the following problems. Place your answers into separate "text" files using the names indicated on each problem. Please create your text files using the same text editor that you use for your .py files. Answer submitted in another file format such as .doc, .pages, .rtf, or.pdf will lose least one point per problem! [1] 3 points Use file math.txt What is the precise output from the following code? bar...

  • Write a C program for: One technique for dealing with deadlock is called “detect and recover.” In...

    Write a C program for: One technique for dealing with deadlock is called “detect and recover.” In this scheme, some procedure is used to identify when a deadlock occurs, and then another procedure is used to deal with the blocked processes. One technique to identify a deadlock is to maintain a resource graph that identifies all processes, all resources, and the relationships between them (that is, which processes exclusively own which resources, and which processes are blocked waiting for which...

  • java Object Oriented Programming The assignment can be done individually or in teams of two. Submit one as...

    java Object Oriented Programming The assignment can be done individually or in teams of two. Submit one assignment per team of two via Omnivox and NOT MIO.Assignments sent via MIO will be deducted marks. Assignments must be done alone or in groups and collaboration between individuals or groups is strictly forbidden. There will be a in class demo on June 1 make sure you are prepared, a doodle will be created to pick your timeslot. If you submit late, there...

  • How can we assess whether a project is a success or a failure? This case presents...

    How can we assess whether a project is a success or a failure? This case presents two phases of a large business transformation project involving the implementation of an ERP system with the aim of creating an integrated company. The case illustrates some of the challenges associated with integration. It also presents the obstacles facing companies that undertake projects involving large information technology projects. Bombardier and Its Environment Joseph-Armand Bombardier was 15 years old when he built his first snowmobile...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT