Uvalive 6255 Kingdoms -- Status search

Source: Internet
Author: User

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

 

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.