ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

【Loj #10100. 「一本通 3.6 练习 1」网络】题解

2021-11-29 18:34:22  阅读:183  来源: 互联网

标签:ch Loj 题解 scanf 3.6 int printf 10100 getchar


题目链接

题目就是给出一幅图,求其割点个数。

由于 \(n\leqslant 100\),所以可以暴力删点。

当然也可以跑割点。

(感谢crx老师教我割点模板)

暴力Code

// Problem: #10100. 「一本通 3.6 练习 1」网络
// Contest: LibreOJ
// URL: https://loj.ac/p/10100
// Memory Limit: 10 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
//#define int long long
inline int read(){int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;
ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+
(x<<3)+(ch^48);ch=getchar();}return x*f;}
//#define mo
#define N 110
struct node
{
	int x, y, n; 
}d[N*N*2]; 
int n, m, i, j, k; 
int x, y, z, ans; 
int h[N], c[N]; 
char ch; 

void cun(int x, int y)
{
	d[++k].x=x; d[k].y=y; 
	d[k].n=h[x]; h[x]=k; 
}

void init()
{
	m=i=j=k=x=y=z=ans=ch=0; 
	memset(h, 0, sizeof(h)); 
	memset(c, 0, sizeof(c)); 
	memset(d, 0, sizeof(d)); 
}

void dfs(int x)
{
	for(int g=h[x]; g; g=d[g].n)
	{
		int y=d[g].y; 
		if(c[y]) continue; 
		c[y]=1; 
		dfs(y); 
	}
}

signed main()
{
//	freopen("tiaoshi.in","r",stdin);
//	freopen("tiaoshi.out","w",stdout);
	n=read(); 
	while(n)
	{
		init(); 
		scanf("%d", &i); 
		while(i) 
		{
			scanf("%d%c", &j, &ch); 
			// printf("%d %d\n", i, j); 
			cun(i, j); cun(j, i); 
			while(ch>=32)
			{
				scanf("%d%c", &j, &ch); 
				// printf("%d %d\n", i, j); 
				cun(i, j); cun(j, i); 
			}
			scanf("%d", &i); 
			// printf("%d\n", i); 
		}
		i=k=0; 
		for(j=1; j<=n; ++j) 
			if(!c[j]) c[j]=1, dfs(j), ++k; 
		// printf("k:%d\n", k); 
		for(i=1; i<=n; ++i)
		{
			memset(c, 0, sizeof(c)); 
			c[i]=1; m=0; 
			for(j=1; j<=n; ++j) 
				if(!c[j]) c[j]=1, dfs(j), ++m; 
			if(m!=k) ++ans; 
			// printf("m:%d\n", m); 
		}
		printf("%d\n", ans); 
		n=read(); 
	}
	return 0;
}


割点Code

// Problem: #10100. 「一本通 3.6 练习 1」网络
// Contest: LibreOJ
// URL: https://loj.ac/p/10100
// Memory Limit: 10 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
//#define int long long
inline int read(){int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;
ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+
(x<<3)+(ch^48);ch=getchar();}return x*f;}
//#define mo
#define N 110
struct node
{
	int x, y, n; 
}d[N*N*2]; 
int n, m, i, j, k; 
int x, y, z, ans; 
int h[N], c[N], f[N], zi[N]; 
char ch; 

void cun(int x, int y)
{
	d[++k].x=x; d[k].y=y; 
	d[k].n=h[x]; h[x]=k; 
}

void init()
{
	m=i=j=k=x=y=z=ans=ch=0; 
	memset(h, 0, sizeof(h)); 
	memset(c, 0, sizeof(c)); 
	memset(d, 0, sizeof(d)); 
	memset(f, 0, sizeof(f)); 
	memset(zi, 0, sizeof(zi)); 
}

void dfs(int x)
{
	for(int g=h[x]; g; g=d[g].n)
	{
		int y=d[g].y; 
		if(!c[y])
		{
			c[y]=c[x]+1;
			dfs(y); 
			if(c[f[y]]<c[f[x]]) f[x]=f[y]; 
			if(c[f[y]]>=c[x]) ++zi[x]; 
		}
		else if(c[y]<c[f[x]]) f[x]=y; 
	}
}



signed main()
{
//	freopen("tiaoshi.in","r",stdin);
//	freopen("tiaoshi.out","w",stdout);
	n=read(); 
	while(n)
	{
		init(); 
		scanf("%d", &i); 
		while(i) 
		{
			scanf("%d%c", &j, &ch); 
			// printf("%d %d\n", i, j); 
			cun(i, j); cun(j, i); 
			while(ch>=32)
			{
				scanf("%d%c", &j, &ch); 
				// printf("%d %d\n", i, j); 
				cun(i, j); cun(j, i); 
			}
			scanf("%d", &i); 
			// printf("%d\n", i); 
		}
		for(i=1; i<=n; ++i) f[i]=i; 
		for(i=1; i<=n; ++i)
			if(!c[i]) c[i]=1, dfs(i); 
		for(i=1; i<=n; ++i)
			if((zi[i]>1)||(f[i]!=i&&zi[i])) ++ans; 
		printf("%d\n", ans); 
		n=read(); 
	}
	return 0;
}


标签:ch,Loj,题解,scanf,3.6,int,printf,10100,getchar
来源: https://www.cnblogs.com/zhangtingxi/p/15620690.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有