Meaning: the debt relationship between countries is given in n countries. if each country has a debt> income, it will go bankrupt. After the bankruptcy, all the debt relationships in the country will be cleared, the first bankrupt country may lead to the last non-bankrupt country, and ask which countries may survive independently.
Solution: because a maximum of 20 cities are involved, the bankruptcy status can be represented by binary numbers. The bankruptcy status is 1, and the bankruptcy status is 0. Then, a search is conducted. Each DFS causes a country to go bankrupt, if there is only one more country left, the country will be pushed into ans.
Code:
# Include <iostream> # include <cstdio> # include <cstring> # include <cmath> # include <algorithm> # include <vector> using namespace STD; # define n 111087int vis [N]; vector <int> ans; int N; int A [23] [23]; void DFS (INT state, int num) {vis [State] = 1; int I, j; If (num = N-1) {for (I = 0; I <n; I ++) if (State & (1 <I) = 0) ans. push_back (I); Return ;}for (I = 0; I <n; I ++) // enumerate the bankrupt countries {If (State & (1 <I) = 0 &&! Vis [state | (1 <I)]) {int debt = 0; For (j = 0; j <n; j ++) {If (State & (1 <j) = 0) // debt + = A [I] [J];} If (debt> 0) // multi-debt DFS (state | (1 <I), num + 1); // Another bankruptcy }}int main () {int T, I, J; scanf ("% d", & T); While (t --) {scanf ("% d", & N); for (I = 0; I <N; I ++) for (j = 0; j <n; j ++) scanf ("% d", & A [I] [J]); memset (VIS, 0, sizeof (VIS); ans. clear (); DFS (0, 0); If (ans. size () = 0) puts ("0"); else {sort (ans. begin (), ans. end (); printf ("% d", ANS [0] + 1); for (I = 1; I <ans. size (); I ++) printf ("% d", ANS [I] + 1); puts ("") ;}} return 0 ;}
View code