【比赛链接】:click here~~
【解题思路】:O(n^2)暴力枚举+unique 函数去重相邻元素。居然只跑了3ms,~~
代码:
<span style="font-size:14px;">// C
#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
using namespace std;
#define rep(i,j,k) for(int i=(int)j;i<(int)k;++i)
#define per(i,j,k) for(int i=(int)j;i>(int)k;--i)
#define lowbit(a) a&-a
#define Max(a,b) a>b?a:b
#define Min(a,b) a>b?b:a
#define mem(a,b) memset(a,b,sizeof(a))
typedef long long LL;
typedef unsigned long long LLU;
typedef double db;
const int N=1e6+10;
const int inf=0x3f3f3f3f;
char str[N];
bool vis[N];
int dir4[4][2]= {{1,0},{0,1},{-1,0},{0,-1}};
int dir8[8][2]= {{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1}};
int movv[5][2]= {{1,0},{0,1},{0,0},{-1,0},{0,-1}};
inline LL read()
{
int c=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-')f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')
{
c=c*10+ch-'0';
ch=getchar();
}
return c*f;
}
char mon1[N],mon2[N];
LL day1,year1;
LL day2,year2;
LL row,line,x,t,y,i,res;
struct node
{
LL codrx;
LL codry;
};
node num[N];
LL mum[N];
int main()
{
LL tot=1;
t=read();
while(t--){
x=read();
int len=0;
for(i=0; i<x; ++i){
num[i].codrx=read();
num[i].codry=read();
}
for(int i=0; i<x; ++i){
for(int j=i+1; j<x; ++j)
mum[len++]=(num[i].codrx-num[j].codrx)*(num[i].codrx-num[j].codrx)+(num[i].codry-num[j].codry)*(num[i].codry-num[j].codry);
}
sort(mum,mum+len);
LL res = unique(mum,mum+len)-mum;
printf("Case %lld: %lld\n",tot++,res+1);
}
return 0;
}</span>
<span style="font-size:14px;">// C
#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
using namespace std;
#define rep(i,j,k) for(int i=(int)j;i<(int)k;++i)
#define per(i,j,k) for(int i=(int)j;i>(int)k;--i)
#define lowbit(a) a&-a
#define Max(a,b) a>b?a:b
#define Min(a,b) a>b?b:a
#define mem(a,b) memset(a,b,sizeof(a))
typedef long long LL;
typedef unsigned long long LLU;
typedef double db;
const int N=1e5;
const int inf=0x3f3f3f3f;
char str[N];
bool vis[N];
int dir4[4][2]= {{1,0},{0,1},{-1,0},{0,-1}};
int dir8[8][2]= {{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1}};
int movv[5][2]= {{1,0},{0,1},{0,0},{-1,0},{0,-1}};
bool leap_year(int y)
{
if(y%4==0&&y%100!=0||y%400==0) return 1;
return 0;
}
inline LL read()
{
int c=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-')f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')
{
c=c*10+ch-'0';
ch=getchar();
}
return c*f;
}
char mon1[N],mon2[N];
LL day1,year1;
LL day2,year2;
LL row,line,x,t,y,i,res;
int main()
{
scanf("%lld",&t);
for(i=1; i<=t; ++i)
{
int res=0;
scanf("%s %lld,%lld",mon1,&day1,&year1);
scanf("%s %lld,%lld",mon2,&day2,&year2);
if((mon1[0]=='J'&&mon1[1]=='a')||mon1[0]=='F') year1=year1;
else year1++;
if((mon2[0]=='J'&&mon2[1]=='a')||mon2[0]=='F'&&day2<=28) year2--;
res=(year2/4)-((year1-1)/4);
res=res-(year2/100)+((year1-1)/100);
res=res+(year2/400)-((year1-1)/400);
printf("Case %lld: %lld\n",i,res);
}
return 0;
}</span>4
1 2
2 1
4 3
3 2
The adjacency list, adj[1]=2, adj[2]=1, adj[3]=2, and adj[4]=3.
Use a boolean array visit[N]
visit[1] | visit[2] | visit[3] | visit[4] |
False | False | False | False |
At first start from 1
If visit[1]==false then run DFS in this time count the visited node and update the visit[] array
(Set visit[i]=True here i is a visited node).
Remember dot not use the array visit[] for cycle finding you can use another boolean array visit2[] for that purpose.
After run DFS the visit[] array is
visit[1] | visit[2] | visit[3] | visit[4] |
True | True | False | False |
Now 2
visit[2]==true so, do not need to do anything.
Now 3
visit[3]==false so, run the DFS. After that the visit[] array is
visit[1] | visit[2] | visit[3] | visit[4] |
True | True | True | False |
Now 4visit[4]==false so, run the DFS. After that the visit[] array is
visit[1] | visit[2] | visit[3] | visit[4] |
True | True | True | True |
For 4, the count_visited_node is maximum. Ans is 4.
<span style="font-size:14px;">#include <stdio.h>
#define MAX 50005
int T,N;
int vis[MAX], f[MAX], c[MAX];
int ans, flag;
typedef long long LL;
inline LL read()
{
int c=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){c=c*10+ch-'0';ch=getchar();}
return c*f;
}
int dfs(int u)
{
int v = f[u];/// 2=f[1];1=f[2];2=f[3];
int r = 0; ///
vis[u] = 1; /// vis[1]=1;vis[2]=1;vis[3]=1;
if(!vis[v]) r = dfs(v) + 1;///vis[2]=1,r=0+1=1;
vis[u] = 0;
c[u] = r; ///c[1]=1,c[2]=0,c[3]=2;
return r;
}
int main()
{
int u,v;
T=read();
for(int t=1; t<=T; t++){
N=read();
for(int i=1; i<=N; i++){
u=read(),v=read();
f[u] = v;
vis[u] = 0;
c[u] = -1;
}
ans = -1;
for(int i=1; i<=N; i++){
if(c[i]==-1) dfs(i);
if(c[i]>m)
{
m=c[i];
flag=i;
}
/* printf("vertex %d children %d\n",i,c[i]);*/
}
printf("Case %d: %d\n",t,flag);
}
return 0;
}</span>7
3
1 2
2 3
3 1
4
1 2
2 1
4 3
3 2
5
1 2
2 1
5 3
3 4
4 5
2
1 2
2 1
3
1 2
2 3
3 1
4
4 2
2 1
4 3
3 2
10
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 1
Case 1: 1
Case 2: 4
Case 3: 3
Case 4: 1
Case 5: 1
Case 6: 4
Case 7: 1
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/u013050857/article/details/47091739