PAT仿真模拟

note1

  1. dfs版本

Code1

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1000;
set<int> save;
vector<int> s;
int  a[maxn][maxn];
int visited[maxn] = {0}, liarnum = 0;
void Dfs(int n, int depth){
	visited[n] = 1;
	save.insert(n + 1);
	for(int i = 0; i < liarnum; i++){
		int id = s[i];
		if(visited[id] == 0 &&( a[n][id] != 0 && a[id][n] != 0)){
			Dfs(id, depth + 1);
		}
	}
}
int main(){
	int num, edge, thres, tempa, tempb, tempc;
	int  gangnum = 0;
	cin >> thres >> num >> edge;
	fill(a[0], a[0] + maxn * maxn, 0);
	for(int i = 0; i < edge; i++){
		cin >> tempa >> tempb >> tempc;
		tempa--; tempb--;
		a[tempa][tempb] += tempc;
	}
	for(int i = 0; i < num; i++){
		int cnt = 0, callbackcnt = 0;
		for(int j = 0 ; j < num; j++){
			if(a[i][j] > 0 && a[i][j] <= 5){
				cnt++;
				if(a[j][i] > 0) callbackcnt++;
			}
			if(cnt > thres && callbackcnt * 1.00 / cnt <= 0.20){
				save.insert(i);
			}
		}
	}
	for(auto it = save.begin(); it != save.end(); it++){
		int temp = *it;
		s.push_back(temp);
	}
	liarnum = s.size();
    if(liarnum == 0){ 
		cout << "None" << endl; 
		return 0;
        
	}
	for(int i = 0; i <  liarnum; i++){
        save.clear();
		if(visited[s[i]] == 0){
			Dfs(s[i], 0);
			//cout << endl;
            for(auto it = save.begin(); it != save.end(); it++){
                if(it == save.begin()) cout << *it;
                else cout << " " << *it;
	        }
            cout << endl;
		}
      
	}
	return 0;
}

Note2

  1. 并查集的写法debug了好久, 一开始一直以为是union的问题, 其实逻辑上的错误主要是在判断两个人是否通话, 而不是上来就判断两个头领是否有通话。 并查集本身没有问题

code2.1

//三月份考试 第三题

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1000;
set<int> save;
vector<int> s;
int  a[maxn][maxn];
int bingcha[maxn] = {-1}, visited[maxn] = {0},  liarnum = 0;
int find(int bingcha[], int n){
	if(bingcha[n] < 0) return n;
	else return bingcha[n] = find(bingcha, bingcha[n]);
}
void Union(int bingcha[], int r1, int r2){
	//if(bingcha[r1] < bingcha[r2]){
    if(r1 < r2) swap(r1, r2);
        
    bingcha[r1] = r2;
	// }else{
    //     if(bingcha[r1] == bingcha[r2]) bingcha[r2]--;
	// 	bingcha[r1] = r2;
	// }
}
int main(){
	int num, edge, thres, tempa, tempb, tempc;
	int  gangnum = 0;
	cin >> thres >> num >> edge;
	fill(a[0], a[0] + maxn * maxn, 0);
	fill(bingcha, bingcha + maxn, -1);
	for(int i = 0; i < edge; i++){
		cin >> tempa >> tempb >> tempc;
		tempa--; tempb--;
		a[tempa][tempb] += tempc;
	}
	for(int i = 0; i < num; i++){
		int cnt = 0, callbackcnt = 0;
		for(int j = 0 ; j < num; j++){
			if(a[i][j] > 0 && a[i][j] <= 5){
				cnt++;
				if(a[j][i] > 0) callbackcnt++;
			}
			if(cnt > thres && callbackcnt * 1.0 / cnt <= 0.2){
				save.insert(i);
			}
		}
	}
	if(save.size() == 0){ 
		cout << "None" ; 
		return 0;
	}
	for(auto it = save.begin(); it != save.end(); it++){
		int temp = *it;
		s.push_back(temp);
	}
	for(int i = 0; i < s.size(); i++){
        int temp1 = find(bingcha, s[i]);
		for(int j = i + 1; j < s.size(); j++){
			int temp2 = find(bingcha, s[j]);
			if(a[temp1][temp2] > 0 && a[temp2][temp1] > 0){
                if(temp2 < temp1) while(1);
                swap(temp1, temp2);
              //  if( temp1 < 15 && temp1 > 10 && temp2 > 15) while(1);
                bingcha[temp2] = temp1;
                cccmt++;
            }
				//Union(bingcha, temp1, temp2);
		}
	}
	for(int j =0; j < s.size(); j++){
        set<int> ans;
		int temp1 = find(bingcha, s[j]);
		if(visited[temp1] == 0){
			visited[temp1] = 1;
			int first = 0;
			for(int i = 0; i < s.size(); i++){
				int temp2 = find(bingcha, s[i]);
				if(temp1 == temp2) {
                    ans.insert(s[i]);
				}
			}
           // sort(ans.begin(), ans.end());
           for(auto it = ans.begin(); it != ans.end(); it++){
                if(it == ans.begin()) cout << *it + 1;
                else cout << " " << *it + 1;    
        	}
			cout << endl;
		}
	
	}
	return 0;
}

code2.2

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1000;
set<int> save;
vector<int> s;
int  a[maxn][maxn];
int bingcha[maxn] = {-1}, visited[maxn] = {0},  liarnum = 0;
int find(int bingcha[], int n){
	if(bingcha[n] < 0) return n;
	else return bingcha[n] = find(bingcha, bingcha[n]);
}
void Union(int bingcha[], int r1, int r2){
   if(r1 < r2) { 
        bingcha[r2] = r1;
	}else{
	    bingcha[r1] = r2;
	}
}
int main(){
#ifdef _DEBUG
	ifstream cin("data.txt");
#endif
	int num, edge, thres, tempa, tempb, tempc;
	int  gangnum = 0;
	cin >> thres >> num >> edge;
	fill(a[0], a[0] + maxn * maxn, 0);
	fill(bingcha, bingcha + maxn, -1);
	for(int i = 0; i < edge; i++){
		cin >> tempa >> tempb >> tempc;
		tempa--; tempb--;
		a[tempa][tempb] += tempc;
	}
	for(int i = 0; i < num; i++){
		int cnt = 0, callbackcnt = 0;
		for(int j = 0 ; j < num; j++){
			if(a[i][j] > 0 && a[i][j] <= 5){
				cnt++;
				if(a[j][i] > 0) callbackcnt++;
			}
			if(cnt > thres && callbackcnt * 1.0 / cnt <= 0.2){
				save.insert(i);
			}
		}
	}
	if(save.size() == 0){ 
		cout << "None" ; 
		return 0;
	}
	for(auto it = save.begin(); it != save.end(); it++){
		int temp = *it;
		s.push_back(temp);
	}
	for(int i = 0; i < s.size(); i++){
        int temp1 = find(bingcha, s[i]);
		for(int j = i + 1; j < s.size(); j++){
			int temp2 = find(bingcha, s[j]);
			if(a[s[i]][s[j]] > 0 && a[s[j]][s[i]] > 0){
                Union(bingcha, temp1, temp2);
                // if(temp2 < temp1) swap(temp1, temp2);
                //     bingcha[temp2] = temp1;   用注释里的效果和union一样
            }
				
		}
	}
	for(int j =0; j < s.size(); j++){
        set<int> ans;
		int temp1 = find(bingcha, s[j]);
		if(visited[temp1] == 0){
			visited[temp1] = 1;
			int first = 0;
			for(int i = 0; i < s.size(); i++){
				int temp2 = find(bingcha, s[i]);
				if(temp1 == temp2) {
                  if(first == 0) { first = 1; cout << s[i] + 1;}
                  else cout << " " << s[i] + 1;    
				}
			}
			cout << endl;
		}
	
	}
#ifdef _DEBUG
	cin.close();
#endif
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值