我部分能够解决这个问题(在一个测试案例中失败了)。我无法弄清楚一个会破坏我的算法的测试用例。
给定图G =(V,E),确定其顶点是否可以分为两组V1和V2,这样V1和V2都在G中形成一个集团。
CodeChef:Problem Link
算法..
a)按照连接顺序对朋友进行排序。 (不计算与自我的联系)
b)将表A(TA)和表B(TB)都设置为空。
bool current_placed = false
For (loop) all the friends in the sorted list do the below
current_placed = false
if (TA empty) {
Place current friend on TA
current_placed = true
} else {
// If current friend is connected with all the people already seated on TA.
if(current friend connected with all on TA){
Place current friend on TA too.
current_placed = true
}
}
if(current_placed == false){
If (TB empty) {
Place current friend on TB
current_placed = true
} else {
// If current friend is connected with all the people already seated on TB
if(current friend connected with all on TB){
Place current friend on TB too.
current_placed = true;
}
}
}
if(current_placed == false) {
ans => NO
break starting for loop
}
// For loop ends
if(current_placed == true)
{
ans => YES
}
我的代码:
#include<bits/stdc++.h>
using namespace std;
typedef vector<int> VI;
typedef set<int> SI;
typedef vector<VI> VVI;
typedef pair<int,int> PII;
typedef vector<PII> VPII;
bool mat[1004][1004];
int contacts[10004];
bool mycomp(const PII &a, const PII &b){
return a.second < b.second;
}
int main(){
int tcs, num_friends, num_pairs;
int a, b;
cin >> tcs;
while(tcs--){
SI TA, TB;
cin >> num_friends >> num_pairs;
memset(mat, 0, sizeof(bool)*1004*1004);
memset(contacts, 0, sizeof(int)*10004);
for(int i=0; i<num_pairs; i++){
cin >> a >> b;
mat[a][b] = true;
mat[b][a] = true;
}
for(int r=1; r<=num_friends; r++){
for(int c=1; c<=num_friends; c++){
if(mat[r][c] and r != c){
contacts[r] += 1;
}
}
}
bool placed = false, connected;
VPII myvec;
for(int i=1; i<=num_friends; i++)
myvec.push_back(PII(i,contacts[i]));
sort(myvec.begin(), myvec.end(), mycomp);
VPII :: iterator it;
for(it = myvec.begin(); it != myvec.end(); ++it){
PII p = *it;
//cout << p.first << " " << p.second << endl;
int i = p.first;
placed = false;
if(TA.empty()){
TA.insert(i);
placed = true;
} else {
connected = true;
SI :: iterator itr;
for(itr = TA.begin(); itr != TA.end(); ++itr){
if(mat[i][*itr] == false){
connected = false;
break;
}
}
if(connected == true){
TA.insert(i);
placed = true;
}
}
if(placed == false){
if(TB.empty()){
TB.insert(i);
placed = true;
} else {
connected = true;
SI :: iterator itr;
for(itr = TB.begin(); itr != TB.end(); ++itr){
if(mat[i][*itr] == false){
connected = false;
break;
}
}
if(connected == true){
TB.insert(i);
placed = true;
}
}
}
if(placed == false){
cout << "NO" << endl;
break;
}
}
if(placed == true)
cout << "YES" << endl;
/*
SI :: iterator itr;
cout << endl;
cout << "SET A" << endl;
for(itr = TA.begin(); itr != TA.end(); ++itr)
cout << *itr << " ";
cout << endl;
cout << "SET B" << endl;
for(itr = TB.begin(); itr != TB.end(); ++itr)
cout << *itr << " ";
cout << endl; */
}
return 0;
}
答案 0 :(得分:0)
不是暗示,而是反驳。
考虑6个顶点图:
(1, 2), (1, 3), (2, 3)
(4, 5), (5, 6), (4, 6)
(1, 4), (2, 3), (5, 6)
每个人有3个连接。请注意,该图表有2个派系(1, 2, 3
和4, 5, 6
)。
现在让我们先说一个人1,然后你把他放在桌子A上。一个人4来到第二,你的算法也将他放在表A.之后就不能放置。
我真的没有办法修复这个贪婪的位置。