PAT A-1142 Maximal Clique

本题链接:1142 Maximal Clique - PAT (Advanced Level) Practice (pintia.cn)

题意:

  给定clique的定义,判断给出的点集是否满足要求

  先判断是否为clique,在判断是否为maximal

代码:

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int mp[210][210];
vector<int> nums;
int nv, ne, m, k;

bool chargeCli() {
	for (int i = 0; i < nums.size(); i++) {
		for (int j = i + 1; j < nums.size(); j++) {
			if (mp[nums[i]][nums[j]] == 0) {
				return false;
			}
		}
	}
	return true;
}

bool chargeMax() {
	bool vis[210];
	fill(vis, vis + 210, false);
	for (int i = 0; i < nums.size(); i++) {
		vis[nums[i]] = true;
	}
	for (int j = 1; j <= nv; j++) {
		if (!vis[j]) {
			bool ans = true;
			for (int i = 0; i < nums.size(); i++) {
				if (!mp[nums[i]][j]) {
					ans = false;
				}
			}
			if (ans) {
				return false;
			}
		}
		
	}
	return true;
}

//clique是无向图中每个点都相邻
//max clique是无法再添加临近点
int main() {
	for (int i = 0; i < 210; i++) {
		for (int j = 0; j < 210; j++) {
			if (i == j) {
				mp[i][j] = 1;
			}
			else {
				mp[i][j] = 0;
			}
			
		}
	}
	
	cin >> nv >> ne;
	for (int i = 0; i < ne; i++) {
		int a, b;
		cin >> a >> b;
		mp[a][b] = mp[b][a] = 1;
	}
	cin >> m;
	for (int i = 0; i < m; i++) {
		cin >> k;
		if (nums.size()) {
			nums.clear();
		}
		for (int j = 0; j < k; j++) {
			int a;
			cin >> a;
			nums.push_back(a);
		}
		if (!chargeCli()) {
			cout << "Not a Clique" << endl;
		}
		else {
			if (!chargeMax()) {
				cout << "Not Maximal" << endl;
			}
			else {
				cout << "Yes" << endl;
			}
		}

	}

	return 0;
}

  • 5
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是参考代码,使用了 C++ STL 中的 sort 函数和双指针来实现: ```c++ #include <iostream> #include <vector> #include <algorithm> #include <cmath> using namespace std; const double EPS = 1e-9; struct Vector { double x, y; double angle; // 极角 }; // 计算点 P 的极角 double polar_angle(double x, double y) { return atan2(y, x); } // 计算向量 AB 的极角 double polar_angle(double Ax, double Ay, double Bx, double By) { return atan2(By - Ay, Bx - Ax); } // 计算向量 AB 的长度 double length(double Ax, double Ay, double Bx, double By) { return hypot(Bx - Ax, By - Ay); } // 计算向量 AB 和向量 CD 的交点 bool intersection(double Ax, double Ay, double Bx, double By, double Cx, double Cy, double Dx, double Dy, double& Px, double& Py) { double d = (Bx - Ax) * (Dy - Cy) - (By - Ay) * (Dx - Cx); if (abs(d) < EPS) return false; // 平行或重合 double t = ((Cx - Ax) * (Dy - Cy) - (Cy - Ay) * (Dx - Cx)) / d; Px = Ax + t * (Bx - Ax); Py = Ay + t * (By - Ay); return true; } // 将向量按照极角排序 bool cmp(const Vector& a, const Vector& b) { return a.angle < b.angle; } int main() { int T; cin >> T; while (T--) { int N; cin >> N; vector<Vector> cucumbers; for (int i = 0; i < N; ++i) { double x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; double len = length(x1, y1, x2, y2); double angle = polar_angle(x1, y1, x2, y2); cucumbers.push_back({x1, y1, angle}); cucumbers.push_back({x2, y2, angle + (angle < 0 ? 2 * M_PI : 0)}); } sort(cucumbers.begin(), cucumbers.end(), cmp); int ans = 0; int left = 0, right = 0; while (left < cucumbers.size()) { while (right < cucumbers.size() && cucumbers[right].angle - cucumbers[left].angle <= M_PI) { ++right; } ans = max(ans, right - left); ++left; } cout << ans << endl; } return 0; } ``` 这个算法的时间复杂度为 $O(n \log n)$,其中 $n$ 是黄瓜的数量。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值