hdu 1689 Alien’s Necklace

输出节点数为奇数且最小环的节点数

开始开2维数组果断超内存,看了别人的代码才知道用奇偶性来标记

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#define maxn 10000+1
using namespace std;
vector<int>mapp[maxn];
int n,m;
int visit[maxn][2];
struct stu
{
	int pre;
	int now;
	int deep;
};
int bfs(int p)
{
	queue<stu>root;
	stu x,y;
	x.pre=-1;
	x.now=p;
	x.deep=0;
	root.push(x);
	while(root.size())
	{
		x=root.front();
		root.pop();
		if(x.now==p&&x.deep>=3&&x.deep%2) return x.deep;
		else if(x.now==p&&x.deep!=0) continue;
		for(int i=0;i<mapp[x.now].size();i++)
		{
			if(mapp[x.now][i]!=x.pre&&!visit[mapp[x.now][i]][(x.deep+1)%2])
			{
				y.pre=x.now;
				y.now=mapp[x.now][i];
				y.deep=x.deep+1;
				visit[y.now][y.deep%2]=1;
				root.push(y);
			}
		}
	}
	return 1<<30;
}
int main()
{
	cin.sync_with_stdio(false);
	int t;
	cin>>t;
	int casee=1;
	while(t--)
	{
		cin>>n>>m;
		for(int i=1;i<=n;i++)  mapp[i].clear();
		for(int i=0;i<m;i++)
		{
			int x,y;
			cin>>x>>y;
			mapp[x].push_back(y);
			mapp[y].push_back(x);
		}
		int ans=1<<30;
		for(int i=1;i<=n;i++)
		{
			memset(visit,0,sizeof(visit));
			ans=min(ans,bfs(i));
		}
		cout<<"Case "<<casee++<<": ";
		if(ans!=1<<30) cout<<"JYY has to use "<<ans<<" balls."<<endl;
		else cout<<"Poor JYY."<<endl;
	}
	return 0;
}
时间: 2024-10-11 18:18:36

hdu 1689 Alien’s Necklace的相关文章

hdu 1689 Alien’s Necklace (bfs层次图剪枝)

Alien's Necklace Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1526    Accepted Submission(s): 415 Problem Description JYY is taking a trip to Mars. To get accepted by the Martians, he decid

hdu 1689 Alien’s Necklace(bfs搜索最小奇数环)

 题意,一个无向图,求该无向图中不小于3节点的最小奇数环. 思路bfs,但因为要求环上点的数目为奇数,所以不能简单的用一个vis数组记录点是否已访问过,可以改成二维的, vis[u][0]表示点在偶数环中出现过,vis[u][1]表示点在奇数环中出现过 #include<cstdio> #include<cstring> #include<cmath> #include<cstdlib> #include<iostream> #include

(hdu 6030) Happy Necklace 找规律+矩阵快速幂

题目链接 :http://acm.hdu.edu.cn/showproblem.php?pid=6030 Problem Description Little Q wants to buy a necklace for his girlfriend. Necklaces are single strings composed of multiple red and blue beads. Little Q desperately wants to impress his girlfriend,

hdu - 1689 Just a Hook (线段树区间更新)

http://acm.hdu.edu.cn/showproblem.php?pid=1698 n个数初始每个数的价值为1,接下来有m个更新,每次x,y,z 把x,y区间的数的价值更新为z(1<=z<=3),问更新完后的总价值. 线段树的区间更新,需要用到延迟标记,简单来说就是每次更新的时候不要更新到底,用延迟标记使得更新延迟到下次需要更新或者询问的时候. 这题只需要输出总区间的信息,即直接输出1结点的信息. 1 #include <iostream> 2 #include <

hdu 1689 Just a Hook

http://acm.hdu.edu.cn/showproblem.php?pid=1698 Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 21930    Accepted Submission(s): 11005 Problem Description In the game of DotA, Pudge’s

HDU 1689 Just a Hook 线段树区间更新求和

点击打开链接 Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 18894    Accepted Submission(s): 9483 Problem Description In the game of DotA, Pudge's meat hook is actually the most horrible

(线段树区间更新)hdu 1689

Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 18759    Accepted Submission(s): 9414 Problem Description In the game of DotA, Pudge’s meat hook is actually the most horrible thing f

hdu 1817 Necklace of Beads(Polya定理)

题目链接:hdu 1817 Necklace of Beads 这题的弱化版:传送门 1 #include<bits/stdc++.h> 2 #define F(i,a,b) for(int i=a;i<=b;++i) 3 using namespace std; 4 typedef long long ll; 5 6 int t,n,m,cas; 7 8 ll mypow(ll a,ll k){ 9 ll an=1; 10 while(k){ 11 if(k&1)an=an*a

hdu 3874 Necklace (树状数组+离线操作)

Necklace Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3923    Accepted Submission(s): 1292 Problem Description Mery has a beautiful necklace. The necklace is made up of N magic balls. Each b