标签:math iostream code color oid cstring ssi scan har
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<vector>
using namespace std;
int n,m,a,b;
int degree[1005];
int vis[1005]={0};
vector<int> vec[1005];
int get=0;
void dfs(int st)
{
vis[st]=1;
get++;
for(int i=0;i<vec[st].size();i++)
{
int v=vec[st][i];
if(!vis[v])
{
dfs(v);
}
}
}
int main()
{
memset(degree,0,sizeof(degree));
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
scanf("%d%d",&a,&b);
vec[a].push_back(b);
vec[b].push_back(a);
degree[a]++;
degree[b]++;
}
int flag=1;
dfs(1);
if(get!=n) flag=0;
for(int i=1;i<=n;i++)
{
if(degree[i]%2!=0)
{
flag=0;
break;
}
if(degree[i]==0)
{
flag=0;
break;
}
}
if(flag) printf("1");
else printf("0");
return 0;
}
思路是把字母看成点,单词看成边。不知道该怎么判断连通性,看了别人用并查集进行判定
#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <string>
#include <cmath>
using namespace std;
int n,chu[200],ru[200],zifu[200],tot,fa[200];
int find(int x)
{
if (x == fa[x])
return x;
return fa[x] = find(fa[x]);
}
bool check(int c)
{
for (int i = 1; i <= tot; i++)
if (zifu[i] == c)
return true;
return false;
}
int main()
{
for (int i = ‘a‘;i <= ‘z‘; i++)
fa[i] = i;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
char s[25];
scanf("%s", s);
int len = strlen(s);
if (!check(s[0]))
zifu[++tot] = s[0];
if (!check(s[len - 1]))
zifu[++tot] = s[len - 1];
ru[s[len - 1]]++;
chu[s[0]]++;
int x = find(s[0]);
int y = find(s[len - 1]);
fa[x] = y;
}
if (n == 1)
printf("Euler path");
else
{
bool flag1 = 0;
int ans = 0;
for (int i = 1; i <= tot; i++)
{
if (find(zifu[i]) != fa[zifu[1]])
{
flag1 = 1; //不连通
break;
}
int t = abs(chu[zifu[i]] - ru[zifu[i]]);
if (t >= 2)
{
//不是欧拉路径
flag1 = 1;
break;
}
if (t == 1)
{
ans++;
}
}
if (flag1)
printf("impossible");
else
if (ans==0)
printf("Euler loop");
else
if (ans == 2)
printf("Euler path");
else
printf("impossible");
}
return 0;
}
标签:math iostream code color oid cstring ssi scan har
原文地址:http://www.cnblogs.com/wangkaipeng/p/6885387.html