提交时间:2023-08-25 09:45:34
运行 ID: 100229
#include <bits/stdc++.h> #define endl '\n' using namespace std; bool mp[300][300]; int ans[300]; bool number(char c) { return ('0'<=c&&c<='9'); } int n,x,num; bool check(int k) { for(int i = 1; i <= n; i++) { if(mp[i][k]&&ans[i]==ans[k]&&k!=i)return false; } return true; } void dfs(int k) { if(k>=n) { for(int i = 1; i <= n; i++) { cout << ans[i] << " "; } cout << endl; exit(0); } for(int i = 1; i <= 4; i++) { ans[k+1]=i; if(check(k+1)) { dfs(k+1); } ans[k+1]=0; } } signed main() { string s; int tmp1,tmp2; cin >> n; getline(cin,s); int sb_bug; for(int i = 1; i <= n; i++) { getline(cin,s); if(number(s[1])) { tmp1 = ((s[0]-'0')*10)+(s[1]-'0'); sb_bug = 3; } else { tmp1 = s[0]-'0'; sb_bug = 2; } tmp2 = 0; s+=' '; //cout << s << endl; for(int j = sb_bug; j < s.length(); j++) { if(number(s[j])) { tmp2 = tmp2*10+(s[j]-'0'); } else { //cout << tmp1 << endl; //cout << tmp2 << endl; //cout << tmp1 << " " << tmp2 << endl; mp[tmp1][tmp2]=true; mp[tmp2][tmp1]=true; tmp2=0; } } } /* for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { cout << mp[i][j] << " "; } cout << endl; } */ dfs(0); return 0; }