# 2018年全国多校算法寒假训练营练习比赛（第四场）（图论）

1
6
……
.##…
……
.#..#.
.#..##
……

Case 1: 3

dfs

#include <cstdio>
#include <iostream>
#include <cstring>
#include <map>
#include <set>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <queue>
#include <cmath>
#include <stack>
#include <ctime>
#include <string>
#include <vector>
#include <sstream>
#include <functional>
#include <algorithm>
using namespace std;

#define mem(a,n) memset(a,n,sizeof(a))
#define memc(a,b) memcpy(a,b,sizeof(b))
#define rep(i,a,n) for(int i=a;i<n;i++) ///[a,n)
#define dec(i,n,a) for(int i=n;i>=a;i--)///[n,a]
#define pb push_back
#define fi first
#define se second
#define IO ios::sync_with_stdio(false)
#define fre freopen("in.txt","r",stdin)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
typedef long long ll;
typedef unsigned long long ull;
const double PI=acos(-1.0);
const double E=2.718281828459045;
const double eps=1e-8;
const int INF=0x3f3f3f3f;
const int MOD=258280327;
const int N=50+5;
const ll maxn=1e6+5;
const int dir[4][2]= {-1,0,1,0,0,-1,0,1};
int n,ans;
char a[N][N];
bool vis[N][N];
bool in(int x,int y)
{
if(x>=0&&x<n&&y>=0&&y<n) return true;
return false;
}
bool dfs(int x,int y)
{
rep(i,0,4)
{
int nx=x+dir[i][0],ny=y+dir[i][1];
if(in(nx,ny) && a[nx][ny]=='#')
{
a[x][y]='.';
if(!dfs(nx,ny))
{
a[nx][ny]='#';
ans++;
return true;
}
a[x][y]='#';
}
}
return false;
}
int main()
{
int T;
scanf("%d",&T);
rep(cas,0,T)
{
scanf("%d",&n);
ans=0;
mem(vis,0);
rep(i,0,n) scanf("%s",a[i]);
rep(i,0,n)
{
rep(j,0,n)
{
if(a[i][j]=='#')
dfs(i,j);
}
}
printf("Case %d: %d\n",cas+1,ans);
}
return 0;
}


64bit IO Format: %lld

20 10 5
1 2 6
1 3 3
1 4 4
1 5 5
2 3 7
2 4 7
2 5 8
3 4 6
3 5 9
4 5 2

Yes

10 2 2
1 2 5
1 2 15

Yes

#include <cstdio>
#include <iostream>
#include <cstring>
#include <map>
#include <set>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <queue>
#include <cmath>
#include <stack>
#include <ctime>
#include <string>
#include <vector>
#include <sstream>
#include <functional>
#include <algorithm>
using namespace std;

#define mem(a,n) memset(a,n,sizeof(a))
#define memc(a,b) memcpy(a,b,sizeof(b))
#define rep(i,a,n) for(int i=a;i<n;i++) ///[a,n)
#define dec(i,n,a) for(int i=n;i>=a;i--)///[n,a]
#define pb push_back
#define fi first
#define se second
#define IO ios::sync_with_stdio(false)
#define fre freopen("in.txt","r",stdin)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
typedef long long ll;
typedef unsigned long long ull;
const double PI=acos(-1.0);
const double E=2.718281828459045;
const double eps=1e-8;
const int INF=0x3f3f3f3f;
const int MOD=258280327;
const int N=1e2+5;
const ll maxn=1e6+5;
const int dir[4][2]= {-1,0,1,0,0,-1,0,1};
int par[N];
struct Node
{
int u,v,w;
bool operator < (const Node& m)const
{
return w<m.w;
}
} a[maxn];
int Find(int x)///找根结点
{
int r=x;
while(par[r]!=r)
r=par[r];
int t=x;
while(t!=r)
{
int fa=par[t];
par[t]=r;
t=fa;
}
return r;
}
bool unite(int x,int y)//合并
{
x=Find(x),y=Find(y);
if(x!=y)
{
par[x]=y;
return true;
}
return false;
}
int main()
{
int n,k=0,m,c;
while(~scanf("%d%d%d",&c,&n,&m))
{
int num=0;
for(int i=1; i<=m; i++)
par[i]=i;
for(int i=1; i<=n; i++)
scanf("%d%d%d",&a[i].u,&a[i].v,&a[i].w);
sort(a,a+n);
int ans=0;
for(int i=1; i<=n; i++)
{
int u=a[i].u,v=a[i].v;
if(unite(u,v)) ans+=a[i].w;
}
printf("%s\n",ans>c?"No":"Yes");
}
return 0;
}

64bit IO Format: %lld

2 3
1 3
1 2 3

1 3

MLE代码：

set<int>st1,st2;
vector<int>ANS;
int main(void)
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
int x;
while(n--) scanf("%d",&x),st1.insert(x);
while(m--) scanf("%d",&x),st2.insert(x);
set_intersection(st1.begin(),st1.end(),st2.begin(),st2.end(),inserter(ANS,ANS.begin()));
int sz=ANS.size();
if(!sz) puts("empty");
else
{
for(int i=0; i<sz-1; i++) printf("%d ",ANS[i]);
printf("%d\n",ANS[sz-1]);
}
st1.clear();
st2.clear();
// printf("%d %d\n",st1.size(),st2.size());
}
return 0;
}

AC代码： 【暴力】

const int N=1e6+5;
const ll maxn=5e4;
const int dir[4][2]= {-1,0,1,0,0,-1,0,1};
int A[N],B[N],result[N];
int main(void)
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
rep(i,0,n) scanf("%d",&A[i]);
rep(i,0,m) scanf("%d",&B[i]);
int i = 0, j = 0;
int cnt = 0;
while(i < n && j < m)
{
if(A[i] == B[j])
{
result[cnt] = A[i];
i++;
j++;
cnt++;
}
else if(A[i] < B[j])
{
i++;
}
else
{
j++;
}
}
if(!cnt) puts("empty");
else
{
for(int i=0; i<cnt-1; i++) printf("%d ",result[i]);
printf("%d\n",result[cnt-1]);
}
}
return 0;
}

64bit IO Format: %lld

const int N=50+5;
const ll maxn=5e4;
const int dir[4][2]= {-1,0,1,0,0,-1,0,1};
bool d[N][N];
int n,m;
void floyd()
{
for(int k=1; k<=n; k++)
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
d[i][j]=d[i][j]||(d[i][k]&&d[k][j]);
}
int main()
{
//fre;
while(~scanf("%d%d",&n,&m))
{
mem(d,0);
rep(i,0,m)
{
int x,y;
scanf("%d%d",&x,&y);
d[x][y]=1;
//d[y][x]=1;
}
floyd();
//for(int i=1;i<=n;i++) printf("d[1][%d]=%d\n",i,d[1][i]);
if(d[1][n]) puts("Yes");
else puts("No");
}
return 0;
}

64bit IO Format: %lld

1~8的全排列，按照全排列的顺序输出，每行结尾无空格。

No_Input

Full arrangement of 1~8

1~3的全排列 ：
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1

int main()
{
int a[8];
rep(i,0,8) a[i]=i+1;
do
{
rep(i,0,8) printf("%d%c",a[i],i==7?'\n':' ');
}
while(next_permutation(a,a+8));
return 0;
}