lightoj 1429 - Assassin`s Creed (II)

题意就是一个组织要杀人,道路是单向的,一个杀人者可以沿着某条路把所经过的点上的人全部杀完,点是可以重复经过的。

问最少需要几个杀人者可以把所有的n个点上的人杀完。。。

这题的难点在于点可以重复经过,而且图中含有环。。。。。

环其实容易想到缩点,,,只是点重复经过怎么处理,因为要用二分图的性质的话,里面的边石不能重复的,所以这里的处理就是添加边进去,如果原图中u到v是可达的,那么u到v就新建一条边。。。。

这样以来就可以用二分图的性质了。。。。

/*****************************************
Author      :Crazy_AC(JamesQi)
Time        :2015
File Name   :
*****************************************/
// #pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <sstream>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <map>
#include <set>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <climits>
using namespace std;
#define MEM(x,y) memset(x, y,sizeof x)
#define pk push_back
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> ii;
typedef pair<ii,int> iii;
const double eps = 1e-10;
const int inf = 1 << 30;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
/**********************Point*****************************/
struct Point{
	double x,y;
	Point(double x=0,double y=0):x(x),y(y){}
};
typedef Point Vector;
Vector operator + (Vector A,Vector B){
	return Vector(A.x + B.x,A.y + B.y);
}
Vector operator - (Vector A,Vector B){//向量减法
	return Vector(A.x - B.x,A.y - B.y);
}
Vector operator * (Vector A,double p){//向量数乘
	return Vector(A.x * p,A.y * p);
}
Vector operator / (Vector A,double p){//向量除实数
	return Vector(A.x / p,A.y / p);
}
int dcmp(double x){//精度正负、0的判断
	if (fabs(x) < eps) return 0;
	return x < 0?-1:1;
}
bool operator < (const Point& A,const Point& B){//小于符号的重载
	return A.x < B.x || (A.x == B.x && A.y < B.y);
}
bool operator == (const Point& A,const Point& B){//点重的判断
	return dcmp(A.x - B.x) == 0&& dcmp(A.y - B.y) == 0;
}
double Dot(Vector A,Vector B){//向量的点乘
	return A.x * B.x + A.y * B.y;
}
double Length(Vector A){//向量的模
	return sqrt(Dot(A,A));
}
double Angle(Vector A,Vector B){//向量的夹角
	return acos(Dot(A,B) / Length(A) / Length(B));
}
double Cross(Vector A,Vector B){//向量的叉积
	return A.x * B.y - A.y * B.x;
}
double Area2(Point A,Point B,Point C){//三角形面积
	return Cross(B - A,C - A);
}
Vector Rotate(Vector A,double rad){//向量的旋转
	return Vector(A.x * cos(rad) - A.y * sin(rad),A.x * sin(rad) + A.y * cos(rad));
}
Vector Normal(Vector A){//法向量
	int L = Length(A);
	return Vector(-A.y / L,A.x / L);
}
double DistanceToLine(Point p,Point A,Point B){//p到直线AB的距离
	Vector v1 = B - A,v2 = p - A;
	return fabs(Cross(v1,v2)) / Length(v1);
}
double DistanceToSegment(Point p,Point A,Point B){//p到线段AB的距离
	if (A == B) return Length(p - A);
	Vector v1 = B - A, v2 = p - A,v3 = p - B;
	if (dcmp(Dot(v1,v2) < 0)) return Length(v2);
	else if (dcmp(Dot(v1,v3)) > 0) return Length(v3);
	else return DistanceToLine(p,A,B);
}
bool SegmentProperIntersection(Point A1,Point A2,Point B1,Point B2){//线段相交
	double c1 = Cross(A2 - A1,B1 - A1),c2 = Cross(A2 - A1,B2 - A1);
	double c3 = Cross(B2 - B1,A1 - B1),c4 = Cross(B2 - B1,A2 - B1);
	return dcmp(c1)*dcmp(c2) < 0 && dcmp(c3)*dcmp(c4) < 0;
}
const int N = 1010;
vector<int> G1[N], G2[N], G3[N];//原图,扩张图,缩点后的图。
bool vis[N];
int pre[N], low[N], Belong[N], scc_cnt, Times;
int n, m;
stack<int> st;
void Tarjan(int u) {
	pre[u] = low[u] = ++Times;
	st.push(u);
	for (int i = 0;i < (int)G2[u].size();++i) {
		int v = G2[u][i];
		if (!pre[v]) {
			Tarjan(v);
			low[u] = min(low[u], low[v]);
		}else if (!Belong[v]) low[u] = min(low[u], pre[v]);
	}
	if (pre[u] == low[u]) {
		scc_cnt++;
		while(true) {
			int x = st.top();
			st.pop();
			Belong[x] = scc_cnt;
			if (x == u) break;
		}
	}
}
void FindSCC() {
	memset(pre, 0,sizeof pre);
	memset(Belong, 0,sizeof Belong);
	Times = scc_cnt = 0;
	for (int i = 1;i <= n;++i) {
		if (!pre[i]) Tarjan(i);
	}
}
void BFS(int st) {
	queue<int> que;
	que.push(st);
	memset(vis, false,sizeof vis);
	vis[st] = true;
	while(!que.empty()) {
		int u = que.front();
		que.pop();
		for (int i = 0;i < G1[u].size();++i) {
			int v = G1[u][i];
			if (vis[v]) continue;
			vis[v] = true;
			G2[st].push_back(v);//对原图进行这种扩展是不会形成可行环的。
			que.push(v);
		}
	}
}
void Initation() {
	for (int i = 1;i <= n;++i)
		BFS(i);
}
void Input() {
	scanf("%d%d",&n,&m);
	for (int i = 1;i <= n;++i)
		G1[i].clear(),G2[i].clear(),G3[i].clear();
	int u, v;
	for (int i = 1;i <= m;++i) {
		scanf("%d%d",&u,&v);
		G1[u].push_back(v);
	}
}
void Trans() {
	for (int u = 1;u <= n;++u) {
		for (int i = 0;i < G2[u].size();++i) {
			int v = G2[u][i];
			if (Belong[u] != Belong[v])
				G3[Belong[u]].push_back(Belong[v]);
		}
	}
}
int linker[N];
bool Search(int u) {
	for (int i = 0;i < G3[u].size();++i) {
		int v = G3[u][i];
		if (vis[v]) continue;
		vis[v] = true;
		if (linker[v] == -1 || Search(linker[v])) {
			linker[v] = u;
			return true;
		}
	}
	return false;
}
int Hungary() {
	int ret = 0;
	memset(linker, -1,sizeof linker);
	for (int i = 1;i <= scc_cnt;++i) {
		memset(vis, false,sizeof vis);
		if (Search(i)) ret++;
	}
	return ret;
}
int main()
{	
	freopen("in.txt","r",stdin);
	// freopen("out.txt","w",stdout);
	int t, icase = 0;
	scanf("%d",&t);
	while(t--) {
		Input();
		Initation();
		FindSCC();
		// printf("SCC = %d\n", scc_cnt);
		// for (int i = 1;i <= n;++i)
			// printf("%d ", Belong[i]);
		// puts("");
		Trans();

		// for (int i = 1;i <= n;++i) {
			// printf("<%d>::", i);
			// for (int j = 0;j < G2[i].size();++j)
				// printf("%d ", G2[i][j]);
			// puts("");
		// }

		// for (int i = 1;i <= scc_cnt;++i) {
		// 	printf("i = %d::::", i);
		// 	for (int j = 0;j < G3[i].size();++j)
		// 		printf("%d ",G3[i][j]);
		// 	puts("");
		// }
		printf("Case %d: %d\n", ++icase, scc_cnt - Hungary());
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值