C. AND Graph+dfs

本文介绍了一个使用深度优先搜索(DFS)算法解决特定图论问题的方法:计算一个由整数通过位与运算(AND)定义的无向图中的连通组件数量。输入包含一组整数,通过位与运算确定节点间的连接,并统计连通区域。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

每次dfs尝试所有可能相连的状态
C. AND Graph
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a set of size m

with integer elements between 0 and 2n1 inclusive. Let's build an undirected graph on these integers in the following way: connect two integers x and y with an edge if and only if x&y=0. Here &

is the bitwise AND operation. Count the number of connected components in that graph.

Input

In the first line of input there are two integers n

and m ( 0n22, 1m2n

).

In the second line there are m

integers a1,a2,,am ( 0ai<2n) — the elements of the set. All ai

are distinct.

Output

Print the number of connected components.

Examples
Input
Copy
2 3
1 2 3
Output
Copy
2
Input
Copy
5 5
5 19 10 20 12
Output
Copy
2
Note

Graph from first sample:

Graph from second sample:

#include <bits/stdc++.h>


typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

using namespace std;


/*
ll pw(ll a, ll b) {
	ll ans = 1; while (b) {
		while (!(b & 1)) b >>= 1, a = (a * a) % MOD;
		ans = (ans * a) % MOD, --b;
	} return ans;
}
*/

int msk;
int n, m;
int fl[1 << 22];
int was[1 << 22];

void dfs1(int v, int c) {
	if (c == 0) {
		fl[v] = 0;
		if (!was[msk - v])
			dfs1(msk - v, 1);
	}
	else {
		was[v] = 1;
		if (fl[v])
			dfs1(v, 0);
		for (int i = 0; i < n; ++i) {
			int x = v & (msk - (1 << i));
			if (!was[x])
				dfs1(x, 1);
		}
	}
}

int main() {
    freopen("in.txt","r",stdin);
	cin >> n >> m;
	msk = (1 << n) - 1;
	for (int i = 0; i < m; ++i) {
		int x;
		cin >> x;
		fl[x] = 1;
	}
	int ans = 0;
	for (int i = 0; i < (1 << 22); ++i) {
		if (fl[i]) {
			dfs1(i, 0), ++ans;
		}
	}
	cout << ans << "\n";
	return 0;
}

给定一个 n×n 的网格状地图,每个方格 (i,j) 有一个高度 wij ​​ 。如果两个方格有公共顶点,则它们是相邻的。 定义山峰和山谷如下: 均由地图上的一个连通块组成; 所有方格高度都相同; 周围的方格(即不属于山峰或山谷但与山峰或山谷相邻的格子)高度均大于山谷的高度,或小于山峰的高度。 求地图内山峰和山谷的数量。特别地,如果整个地图方格的高度均相同,则整个地图既是一个山谷,也是一个山峰。 【输入】 第一行一个整数n(2≤n≤1000) ,表示地图的大小。 接下来 n 行每行 n 个整数表示地图。第 i 行有 n 个整数 wi1,wi2,…,win(0≤wij≤1000000000) ,表示地图第 i 行格子的高度。 【输出】 输出一行两个整数,分别表示山峰和山谷的数量。 #include<iostream> #include<vector> #include<queue> #include<tuple> #include<set> #include<unordered_map> #include<unordered_set> using namespace std; int mh, mg,n; int a[1001][1001] = {0}; const int dy[8] = { 1,1,1,0,0,-1,-1,-1 }; const int dx[8] = { 1,0,-1,1,-1,1,0,-1 }; struct dot { int num,x,y; int small=0, big=0; dot() {} }; struct pointhash { size_t operator()(const pair<int, int>& p)const{ return hash<int>()(p.first) ^( hash<int>()(p.second) << 1); } }; unordered_set<pair<int, int>, pointhash>p; void dfs(vector<vector<dot>>&graph,vector<vector<int>>visit) { queue<pair<int,int>>heap; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { bool find = true; int lei = 0; if (visit[i][j])continue; heap.push({i,j}); while (!heap.empty()) { auto [v,u] = heap.front(); heap.pop(); visit[v][u] = 1; for (int k = 0; k < 8; k++) { int newx = v + dx[k]; int newy = u + dy[k]; if (newx < 0 || newy < 0 || newx >= n || newy >= n || visit[newx][newy])continue; if (p.find({ v + newx * n, u + newy * n }) != p.end())continue; if (graph[v][u].num > graph[newx][newy].num) { graph[v][u].big++, graph[newx][newy].small++; p.insert({ v +u * n, newx + newy * n }); p.insert({ newx + newy * n, v + u * n }); lei = 1; } else if (graph[v][u].num < graph[newx][newy].num) { graph[v][u].small++, graph[newx][newy].big++; p.insert({ v + u * n, newx + newy * n }); p.insert({ newx + newy * n, v + u * n }); lei = 2; } else heap.push({newx,newy}); } if (graph[v][u].small && graph[v][u].big)find = false; } if (find) { if (lei==1) mg++; else mh++; } } } } int main() { cin >> n; vector<vector<dot>>graph(n, vector<dot>(n)); vector<vector<int>>visit(n, vector<int>(n)); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> graph[i][j].num; graph[i][j].x = i; graph[i][j].y = j; } } dfs(graph, visit); cout << mh << " " << mg; } 哪里可以改进
最新发布
03-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值