L2-025 分而治之 (25 分)

我看这道题的时候想到的是天梯红色警报这道题,都考察了连通性问题,但这么写了一个超内存了hhh 改了半天也没改成功 应该是复原方法有问题就不该用二维数组存 后来查了优秀代码
用的是度、邻接表 开个一维数组来存储每个节点的度 这样空间就是一维 不会超内存了
AC代码:

#include <iostream>
#include <cstring>
#include <vector>
#include <stack>
#include <algorithm>
using namespace std;
const int maxn = 10010;

//度的写法
int n,m,k,x,y;
vector<int> v[maxn];
int cnt[maxn],c[maxn];
int main()
{
	cin >> n >> m;
	while(m--)
	{
		cin >> x >> y;
		v[x].push_back(y);
		v[y].push_back(x);
		cnt[x]++;
		cnt[y]++;
	}
	cin >> k;
	while(k--)
	{
		cin >> x;
		for(int i = 1; i <= n; i++) //复原
		{
			c[i] = cnt[i];
		}
		while(x--)
		{
			cin >> y;
			c[y] = 0;
			for(int i = 0; i < v[y].size(); i++)
			{
				c[v[y][i]]--;
			}
		}
		bool flag = true;
		for(int i = 1; i <= n; i++)
		{
			if(c[i] > 0) flag = false;
		}
		if(!flag) cout << "NO" << endl;
		else cout << "YES" << endl;
	}
	return 0;
}

错误写法:

#include <iostream>
#include <cstring>
#include <vector>
#include <stack>
#include <algorithm>
using namespace std;
const int maxn = 10010;
int n,m;
int G[maxn][maxn],G1[maxn][maxn];
bool visit[maxn];
void bfs(int index)
{
	visit[index] = true;
	for(int i = 1; i <= n; i++)
	{
		if(visit[i] == false && G1[index][i] == 1) visit[i] = true;
	}
}
int getcont()
{
	int cont = 0;
	fill(visit,visit + maxn, false);
	for(int i = 1; i <= n; i++)
	{
		if(visit[i] == false)
		{
			cont++;
			bfs(i);
		}
	}
	return cont;
}
int main()
{
	int a,b,k,g,u;
	cin >> n >> m;
	while(m--)
	{
		cin >> a >> b;
		G[a][b] = G[b][a] = 1;
	}
	cin >> k;
	while(k--)
	{
		for(int i = 1; i <= n; i++)
		{
			for(int j = 1; j <= n; j++)
			{
				G1[i][j] = G[i][j];
			}
		}
		int cont = 0;
		cin >> g;
		while(g--)
		{
			cin >> u;
			for(int i = 1; i <= n; i++)
			{
				if(G1[u][i] == 1)
				{
					G1[u][i] = G1[i][u] = 0;//断点
				}
			}
			 cont = getcont();
		}
		if(cont != n) cout <<"NO" << endl;
		else cout << "YES" << endl;
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

moumoumouwang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值