Mr. Dajuda, who is famous for a TV show program, occasionally suggests an interesting game for the audience and gives them some gifts as a prize. The game he suggested this week can be explained as follows.c++
The k(> 3) lamps on the stage are all turned off at the beginning of the game. For convenience, lamps are numbered from 1 to k. Each lamp has a color, either red or blue. However, the color of a lamp cannot be identified until it is turned on. Game participants are asked to select three lamps at random and to guess the colors of them. Then each participant submits a paper on which the predicted colors of selected lamps are recorded to Mr. Dajuda, the game host. When all the lamps are turned on, each participant checks how many predicted colors match the actual colors of the lamps. If two or more colors match, he/she will receive a nice gift as a prize.dom
Mr. Dajuda prepared a special gift today. That is, after reviewing all the papers received from the game participants he tries to adjust the color of each lamp so that every participant can receive a prize if possible.ide
Given information about the predicted colors as explained above, write a program that determines whether the colors of all the lamps can be adjusted so that all the participants can receive prizes.this
Your program is to read from standard input. The input starts with a line containing two integers, k and n (3 < k ≤ 5,000, 1 ≤ n ≤ 10,000), where k is the number of lamps and n the number of game participants. Each of the following n lines contains three pairs of (l, c), where l is the lamp number he/she selected and c is a character, either B for blue or R for red, which denotes the color he/she guessed for the lamp. There is a blank between l and c and each pair of (l, c) is separated by a blank as well as shown in following samples.spa
Your program is to write to standard output. If it is possible that all the colors can be adjusted so that every participant can receive a prize, print k characters in a line. The ith character, either B for blue or R for red represents the color of the ith lamp. If impossible, print -1. If there are more than one answer, you can print out any of them..net
7 5 3 R 5 R 6 B 1 B 2 B 3 R 4 R 5 B 6 B 5 R 6 B 7 B 1 R 2 R 4 R
BRRRBBB
有k個燈,5個嘉賓,每一個嘉賓會選擇3個燈進行猜顏色(只有紅色和藍色),猜中兩個以上有獎,問怎麼設置燈的顏色能使全部嘉賓都能得獎。調試
比賽的時候 和zn一塊兒 寫的暴搜 調試改錯 花了兩個小時吧 終於能過樣例了 然而結果確定是超時了rest
正解是 創建圖論裏的2-SAT模型code
關於什麼是2 - SAT 能夠看看大神的講解 https://blog.csdn.net/jarjingx/article/details/8521690orm
研究了一天多...目前仍是半知半解,等徹底明白了回來更新
照着題解寫了個AC代碼
#include<bits/stdc++.h> using namespace std; #define rep(i,a,n) for(int i=a;i<n;++i) #define read(x) scanf("%d",&x) #define read2(x,y) scanf("%d%d",&x,&y) #define pb push_back #define mp make_pair typedef pair<int,int> P; typedef long long ll; const int maxn = 10005; const int maxm = 5005; int T; int n,k; int tmp; int vis[maxn * 2]; vector<int> sb[maxn],dd[maxn]; map<char,int>c; stack<int> st; int dfs(int u){ if(vis[u]) return 1; if(vis[u ^ 1]) return 0; vis[u] = 1; st.push(u) ; for(int i = 0; i < dd[u].size(); i++){ int v = dd[u][i]; for(int j = 0; j < sb[v].size(); j++){ int g = sb[v][j]; if(u != g){ if(!dfs(g ^ 1)) return 0; } } } return 1; } int _2sat(){ memset(vis,0,sizeof vis); for(int i = 2; i <= k * 2; i += 2){ if(vis[i] || vis[i ^ 1]) continue; while(!st.empty()) st.pop(); if(!dfs(i)) { while(!st.empty()){ vis[st.top()] = 0; st.pop(); } if(!dfs(i ^ 1)) return 0; } } return 1; } int main() { c['R'] = 0; c['B'] = 1; int pos; char col; read2(k,n); for(int i = 1; i < maxn ;i ++) { dd[i].clear(); sb[i].clear(); } for(int i = 1; i <= n; i++){ for(int j = 0; j < 3; j++){ scanf("%d %c",&pos,&col); tmp = pos * 2 + (c[col] ^ 1); sb[i].pb(tmp); dd[tmp].pb(i); } } if(_2sat()){ for(int i = 1; i <= k; i++){ if(vis[i * 2]) printf("R"); else printf("B"); } printf("\n"); } else printf("-1"); return 0; }