Test instructions: Give a graph with n points, each point has a number of stones, now the game rule is to set a point a of the degree of D, if a point of the number of stones greater than or equal to D, you can from a point to each adjacency dots a stone. If the game can play more than 100,000 times, output the INF, otherwise the output can play a few times.
Idea: Violent enumeration of each playable point, if you can play an unlimited number of times, and the current state is Z (refers to all points of the sequence of stone numbers as a state), then after playing several times, it will cause a cycle, that is, after several times, each point of the stone number and the initial number of stones exactly the same, In this way I repeat how the previous play can be unlimited play. However, because there are 200 points, so play once to determine whether the same as the initial state, otherwise the complexity of O (100000* nodes * The number of sides of the node), the order of magnitude billion.
Now you only need to enumerate 100,000 times to know the result, not to judge the other. And every time you choose to play the point can be arbitrarily selected, as long as the conditions are met (do not need to play a number of stones, or less).
1#include <bits/stdc++.h>2 #defineLL Long Long3 using namespacestd;4 Const intn=205;5 intN, M, A, B, num[n];6vector< vector<int> >Vect;7unordered_set<int>sett;8 9 intcal ()Ten { One intCnt=0; A for(intI=0; i<n; i++)if(Num[i]>=vect[i].size ()) Sett.insert (i);//Meet the criteria - while(!sett.empty ()) - { thecnt++;//One time - if(cnt>=100000)returnCNT; - intq=*Sett.begin (); -num[q]-=vect[q].size (); + if(Num[q]<vect[q].size ()) Sett.erase (Sett.begin ());//this point is no longer playable. - + for(intI=0; I<vect[q].size (); i++)//Add stones to the neighbours A { at intP=vect[q][i];//the point connected to the top node of the stack . -num[p]++; - if(num[p]>=vect[p].size ()) Sett.insert (p); - } - } - returnCNT; in } - to intMain () + { - //freopen ("E://input.txt "," R ", stdin); thescanf"%d%d",&n,&m); * for(intI=0; i<n; i++) scanf ("%d", &num[i]);//Number of stones $ vect.resize (n);Panax Notoginseng for(intI=0; i<m; i++) - { thescanf"%d%d", &a, &b);//input side, no direction graph + Vect[a].push_back (b); A Vect[b].push_back (a); the } + - intcn1=cal (); $ if(ans>=100000) printf ("inf\n"); $ Elseprintf"%d\n", ans); - - return 0; the}
AC Code
Hihocoder #1179: Eternity Games (brute Force enumeration)