2021级新生个人训练赛第40场

问题 A: 多项式输出

题目描述
一元 n 次多项式可用如下的表达式表示:
其中,aixi称为 i 次项,ai 称为 i 次项的系数。给出一个一元多项式各项的次数和系数,请按照如下规定的格式要求输出该多项式:
f(x)=anxn+an-1xn-1+…+a1x+a0,an≠0

  1. 多项式中自变量为 x,从左到右按照次数递减顺序给出多项式。
  2. 多项式中只包含系数不为 0 的项。
  3. 如果多项式 n 次项系数为正,则多项式开头不出现“+”号,如果多项式 n 次项系数为负,则多项式以“-”号开头。
  4. 对于不是最高次的项,以“+”号或者“-”号连接此项与前一项,分别表示此项系数为正或者系数为负。紧跟一个正整数,表示此项系数的绝对值(如果一个高于 0 次的项,其系数的绝对值为 1,则无需输出 1)。如果 x 的指数大于 1,则接下来紧跟的指数部分的形式为“x^b”,其中 b 为 x 的指数;如果 x 的指数为 1,则接下来紧跟的指数部分形式为“x”;
    如果 x 的指数为 0,则仅需输出系数即可。
  5. 多项式中,多项式的开头、结尾不含多余的空格。
    输入
    输入共有 2 行
    第一行 1 个整数,n,表示一元多项式的次数。
    第二行有 n+1 个整数,其中第 i 个整数表示第 n-i+1 次项的系数,每两个整数之间用空格隔开。(0<=n<=100,-100<=系数<=100)
    输出
    输出共 1 行,按题目所述格式输出多项式。
    样例输入 Copy
    5
    100 -1 1 -3 0 10
    样例输出 Copy
    100x5-x4+x3-3x2+10

模拟题,没啥好说的,慢慢写判断就行

#pragma GCC optimize(1)
#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
# include<bits/stdc++.h>
# include<unordered_map>
 
# define eps 1e-9
# define fi first
# define se second
# define ll long long
# define int ll
// cout<<fixed<<setprecision(n) 
//bool operator<(const Node& x )
using namespace std;
typedef unsigned long long ull;
typedef pair<int,int > PII; 
const int mod=1e9+7;
const int N=2e6+10;
const int Time=86400;
const int X=131;
const int inf=0x3f3f3f3f;
const double PI = 1e-4;
double pai = 3.14159265358979323846; 
double e = exp(1);
 
int T,n,m,l,r,d,ans,maxn,sum,x,y;
map<int,int>mp;
int a[N],s1[N],s2[N];
void solve(){
        cin >> n;
          for( int i=n;i>=0;i--){
              cin >> x;
              if(x==0)  {continue;}
               
              if(i!=n&&x>0&&y)          cout<<"+"; 
              if(x<-1||x>1||i==0)     cout<<x;  
              if(x==-1&&i)           cout<<"-";      
             if(i==1)               cout<<"x"; 
             if(i>1)               cout<<"x^"<<i; 
           y++;
          }
}
/*
 
*/
signed main(){  
    std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); 
  // cin >> T;
       T = 1;
    while(T--){
        solve();
    } 
    return 0; 
}

问题 B: 分数线划定

题目描述
世博会志愿者的选拔工作正在 A 市如火如荼的进行。为了选拔最合适的人才,A 市对所有报名的选手进行了笔试,笔试分数达到面试分数线的选手方可进入面试。面试分数线根据计划录取人数的150%划定,即如果计划录取m名志愿者,则面试分数线为排名第m*150%
(向下取整)名的选手的分数,而最终进入面试的选手为笔试成绩不低于面试分数线的所有选手。

现在就请你编写程序划定面试分数线,并输出所有进入面试的选手的报名号和笔试成绩。
输入
第一行,两个整数 n,m(5 ≤ n ≤ 5000,3 ≤ m ≤ n),中间用一个空格隔开,其中 n 表示报名参加笔试的选手总数,m 表示计划录取的志愿者人数。输入数据保证 m*150%向下取整后小于等于 n。
第二行到第 n+1 行,每行包括两个整数,中间用一个空格隔开,分别是选手的报名号 k(1000 ≤ k ≤ 9999)和该选手的笔试成绩 s(1 ≤ s ≤ 100)。数据保证选手的报名号各不相同。
输出
第一行,有两个整数,用一个空格隔开,第一个整数表示面试分数线;第二个整数为进入面试的选手的实际人数。
从第二行开始,每行包含两个整数,中间用一个空格隔开,分别表示进入面试的选手的报名号和笔试成绩,按照笔试成绩从高到低输出,如果成绩相同,则按报名号由小到大的顺序输出。
样例输入 Copy
6 3
1000 90
3239 88
2390 95
7231 84
1005 95
1001 88
样例输出 Copy
88 5
1005 95
2390 95
1000 90
1001 88
3239 88

思路:排序,然后先找到第m*1.5这个人的分数,然后看有没有同分的,接着把这些人输出就行

#pragma GCC optimize(1)
#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
# include<bits/stdc++.h>
# include<unordered_map>
 
# define eps 1e-9
# define fi first
# define se second
# define ll long long
# define int ll
// cout<<fixed<<setprecision(n) 
//bool operator<(const Node& x )
using namespace std;
typedef unsigned long long ull;
typedef pair<int,int > PII; 
const int mod=1e9+7;
const int N=1e6+10;
const int Time=86400;
const int X=131;
const int inf=0x3f3f3f3f;
const double PI = 1e-4;
double pai = 3.14159265358979323846; 
double e = exp(1);
 
int T,n,m,l,r,d,ans,maxn;
map<int,int>mp;
struct Node{
    int xh;
    int s;
 
}a[N];
bool cmp(Node a,Node b){
      if(a.s == b.s) return a.xh < b.xh;
      return a.s > b.s;
}
void solve(){
        cin >> n >> m;
        m = m * 1.5;
        for(int i = 1 ; i <= n ; i ++ ) cin >> a[i].xh >> a[i].s;
        sort(a+1,a+1+n,cmp);
        int minn = a[m].s;
 
        for(int i = m + 1; i <= n ; i ++ )
         if(a[i].s == minn) m++;
          else break;
          cout<<minn<<" "<<m<<"\n";
           
         for(int i = 1 ; i <= m ; i ++) cout<<a[i].xh<<" "<<a[i].s<<"\n";
}
/*
 
*/
signed main(){  
    std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); 
  // cin >> T;
       T = 1;
    while(T--){
        solve();
    } 
    return 0; 
}

问题 C: 细胞分裂

题目描述
Hanks 博士是 BT (Bio-Tech,生物技术) 领域的知名专家。现在,他正在为一个细胞实验做准备工作:培养细胞样本。

Hanks 博士手里现在有 N 种细胞,编号从 1~N,一个第 i 种细胞经过 1 秒钟可以分裂为Si个同种细胞(Si为正整数)。现在他需要选取某种细胞的一个放进培养皿,让其自由分裂,进行培养。一段时间以后,再把培养皿中的所有细胞平均分入 M 个试管,形成 M 份样本,用于实验。Hanks 博士的试管数 M 很大,普通的计算机的基本数据类型无法存储这样大的M 值,但万幸的是,M 总可以表示为 m1的 m2次方,即M = m1^m2,其中 m1,m2均为基本数据类型可以存储的正整数。

注意,整个实验过程中不允许分割单个细胞,比如某个时刻若培养皿中有 4 个细胞,Hanks 博士可以把它们分入 2 个试管,每试管内 2 个,然后开始实验。但如果培养皿中有 5个细胞,博士就无法将它们均分入 2 个试管。此时,博士就只能等待一段时间,让细胞们继续分裂,使得其个数可以均分,或是干脆改换另一种细胞培养。

为了能让实验尽早开始,Hanks 博士在选定一种细胞开始培养后,总是在得到的细胞“刚好可以平均分入 M 个试管”时停止细胞培养并开始实验。现在博士希望知道,选择哪种细胞培养,可以使得实验的开始时间最早。
输入
共有三行。
第一行有一个正整数 N(1 ≤N≤ 10000),代表细胞种数。
第二行有两个正整数 m1,m2(1 ≤m1 ≤ 30000,1 ≤m2 ≤ 10000),以一个空格隔开, m1m2即表示试管的总数M。
第三行有 N 个正整数,第i 个数Si(1 ≤ Si ≤ 2,000,000,000) 表示第i 种细胞经过1 秒钟可以分裂成同种细胞的个数。
输出
共一行,为一个整数,表示从开始培养细胞到实验能够开始所经过的最少时间(单位为秒)。
如果无论 Hanks 博士选择哪种细胞都不能满足要求,则输出整数-1。
样例输入 Copy
1
2 1
3
样例输出 Copy
-1
提示
经过 1 秒钟,细胞分裂成 3 个,经过 2 秒钟,细胞分裂成 9 个,……,可以看出无论怎么分裂,细胞的个数都是奇数,因此永远不能分入 2 个试管。

思路:分解质因数
小tips:m1^n ,不论n多大,质因数永远只有m1
所以我们可以先将m1进行质因数分解
然后枚举i,对第i个细胞进行质因数分解,如果容器能包含所有si的质因数那就更新最大值

#pragma GCC optimize(1)
#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
# include<bits/stdc++.h>
# include<unordered_map>

# define eps 1e-9
# define fi first
# define se second
# define ll long long
# define int ll
// cout<<fixed<<setprecision(n) 
//bool operator<(const Node& x )
using namespace std;
typedef unsigned long long ull;
typedef pair<int,int > PII; 
const int mod=1e9+7;
const int N=2e6+10;
const int Time=86400;
const int X=131;
const int inf=0x3f3f3f3f;
const double PI = 1e-4;
double pai = 3.14159265358979323846; 
double e = exp(1);

int T,n,m,m1,m2,maxn,ans;
int f[N];
int s[N],p[N];
int t = 2;
void solve(){
	       cin >> n >> m1 >> m2;
	       for(int i = 1 ; i <= n ; i ++ ) cin >> s[i];
	       if(m1 == 1){cout<<"0\n";return ;}
	       while(m1 != 1){
		   	   while(m1%t == 0) m1 /= t , p[t]++;
		   	   maxn = max(maxn,t);
		   	   p[t++]*=m2;
		   }
		   ans = inf;
	       for(int i = 1 ; i <= n ; i ++ ){
		   	   int l = 0;
		   	   for(int j = 2 ; j <= maxn ; j ++ ){
				  	   if(!p[j]) continue;
				  	   int c = 0;
				  	   while(s[i] % j == 0) s[i]/=j,c++;
				  	   if(c == 0) {l = inf ; break;}
				  	   l = max(l,(p[j]-1)/c);
				  }
				  ans = min(ans,l);
		   }
		  if(ans >= inf) cout<<-1<<"\n";
		   else cout<<ans+1;
}
/*

*/
signed main(){  
    std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); 
  // cin >> T;
       T = 1;
    while(T--){
		solve();
	} 
    return 0; 
}

问题 D: 道路游戏

题目描述
小新正在玩一个简单的电脑游戏。
游戏中有一条环形马路,马路上有 n 个机器人工厂,两个相邻机器人工厂之间由一小段马路连接。小新以某个机器人工厂为起点,按顺时针顺序依次将这 n 个机器人工厂编号为1~n,因为马路是环形的,所以第 n 个机器人工厂和第 1 个机器人工厂是由一段马路连接在一起的。小新将连接机器人工厂的这 n 段马路也编号为 1~n,并规定第 i 段马路连接第 i 个机器人工厂和第 i+1 个机器人工厂(1≤i≤n-1),第 n 段马路连接第 n 个机器人工厂和第 1个机器人工厂。
游戏过程中,每个单位时间内,每段马路上都会出现一些金币,金币的数量会随着时间发生变化,即不同单位时间内同一段马路上出现的金币数量可能是不同的。小新需要机器人的帮助才能收集到马路上的金币。所需的机器人必须在机器人工厂用一些金币来购买,机器人一旦被购买,便会沿着环形马路按顺时针方向一直行走,在每个单位时间内行走一次,即从当前所在的机器人工厂到达相邻的下一个机器人工厂,并将经过的马路上的所有金币收集给小新,例如,小新在 i(1≤i≤n)号机器人工厂购买了一个机器人,这个机器人会从 i 号机器人工厂开始,顺时针在马路上行走,第一次行走会经过 i 号马路,到达 i+1 号机器人工厂(如果 i=n,机器人会到达第 1 个机器人工厂),并将 i 号马路上的所有金币收集给小新。 游戏中,环形马路上不能同时存在 2 个或者 2 个以上的机器人,并且每个机器人最多能够在环形马路上行走 p 次。小新购买机器人的同时,需要给这个机器人设定行走次数,行走次数可以为 1~p 之间的任意整数。当马路上的机器人行走完规定的次数之后会自动消失,小新必须立刻在任意一个机器人工厂中购买一个新的机器人,并给新的机器人设定新的行走次数。
以下是游戏的一些补充说明:
游戏从小新第一次购买机器人开始计时。
购买机器人和设定机器人的行走次数是瞬间完成的,不需要花费时间。
购买机器人和机器人行走是两个独立的过程,机器人行走时不能购买机器人,购买完机器人并且设定机器人行走次数之后机器人才能行走。
在同一个机器人工厂购买机器人的花费是相同的,但是在不同机器人工厂购买机器人的花费不一定相同。
购买机器人花费的金币,在游戏结束时再从小新收集的金币中扣除,所以在游戏过程中小新不用担心因金币不足,无法购买机器人而导致游戏无法进行。也因为如此,游戏结束后,收集的金币数量可能为负。
现在已知每段马路上每个单位时间内出现的金币数量和在每个机器人工厂购买机器人需要的花费,请你告诉小新,经过 m 个单位时间后,扣除购买机器人的花费,小新最多能收集到多少金币。
输入
第一行 3 个正整数,n,m,p,意义如题目所述。
接下来的 n 行,每行有 m 个正整数,每两个整数之间用一个空格隔开,其中第 i 行描述了 i 号马路上每个单位时间内出现的金币数量(1≤金币数量≤100),即第 i 行的第 j(1≤j≤m)个数表示第 j 个单位时间内 i 号马路上出现的金币数量。
最后一行,有 n 个整数,每两个整数之间用一个空格隔开,其中第 i 个数表示在 i 号机器人工厂购买机器人需要花费的金币数量(1≤金币数量≤100)。
输出
共一行,包含 1 个整数,表示在 m 个单位时间内,扣除购买机器人花费的金币之后,小新最多能收集到多少金币。
样例输入 Copy
2 3 2
1 2 3
2 3 4
1 2
样例输出 Copy
5

思路:f[i]:第i秒获得的最大钱数
转移方程 f[i] = max(f[i],f[i-k] - a[last] + sum) 1<=k<=p
a[i] : 第i个工厂的机器人的费用
sum是到第k秒内所有的金币数 然后每次减去第last个工厂的价格
如果i-k 就break 因为时间不能为负数
总结 枚举时间i 1~m
枚举工厂j 1~n
对于第i时间下第j个工厂枚举所有满足的k 1~max(i,p)
更新f[i]的值
输入f[m]即为最大值 时间复杂度为O(n^3)

#pragma GCC optimize(1)
#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
# include<bits/stdc++.h>
# include<unordered_map>

# define eps 1e-9
# define fi first
# define se second
# define ll long long
# define int ll
// cout<<fixed<<setprecision(n) 
//bool operator<(const Node& x )
using namespace std;
typedef unsigned long long ull;
typedef pair<int,int > PII; 
const int mod=1e9+7;
const int N=2e6+10;
const int Time=86400;
const int X=131;
const int inf=0x3f3f3f3f;
const double PI = 1e-4;
double pai = 3.14159265358979323846; 
double e = exp(1);

int T,n,m,l,r,d,ans,maxn,sum,x,y,p;
int f[N];
int a[N];
int b[1005][1005];
void solve(){
	      cin >> n >> m >> p;
	      memset(f,-inf,sizeof f);
	      f[0] = 0;
	      for(int i = 1 ; i <= n ; i ++)
	       for(int j = 1 ; j <= m ; j ++)
	        cin >> b[i][j];
	      for(int i = 1 ; i <= n ; i ++ ) cin >> a[i];
	      
	      for(int i = 1 ; i <= m ; i ++ )
	       for(int j = 1 ; j <= n ; j ++ ){
		   	    int ff = j - 1;
		   	    if(!ff) ff = n;
		   	    int ss = b[ff][i];
		   	    for(int k = 1 ; k <= p ; k ++){
				   	   if(i-k < 0) break;
				   	   f[i] = max(f[i],f[i-k] + ss - a[ff]);
				   	   ff--;
				   	   if(!ff)  ff = n;
				   	   ss += b[ff][i-k];
				   }
		   }
		   cout<<f[m];
	      
	     
}
/*

*/
signed main(){  
    std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); 
  // cin >> T;
       T = 1;
    while(T--){
		solve();
	} 
    return 0; 
}

问题 E: Sharing Cookies

题目描述
Snuke is giving cookies to his three goats.
He has two cookie tins. One contains A cookies, and the other contains B cookies. He can thus give A cookies, B cookies or A+B cookies to his goats (he cannot open the tins).
Your task is to determine whether Snuke can give cookies to his three goats so that each of them can have the same number of cookies.

Constraints
1≤A,B≤100
Both A and B are integers.
输入
Input is given from Standard Input in the following format:
A B
输出
If it is possible to give cookies so that each of the three goats can have the same number of cookies, print Possible; otherwise, print Impossible.
样例输入 Copy
4 5
样例输出 Copy
Possible
提示
If Snuke gives nine cookies, each of the three goats can have three cookies.

思路:只要a,b ,a+b有一个能被三整除就是Possible,否则就是Impossible
不要单纯的以为只是a+b

#pragma GCC optimize(1)
#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
# include<bits/stdc++.h>
# include<unordered_map>
 
# define eps 1e-9
# define fi first
# define se second
# define ll long long
# define int ll
// cout<<fixed<<setprecision(n) 
//bool operator<(const Node& x )
using namespace std;
typedef unsigned long long ull;
typedef pair<int,int > PII; 
const int mod=1e9+7;
const int N=1e6+10;
const int Time=86400;
const int X=131;
const int inf=0x3f3f3f3f;
const double PI = 1e-4;
double pai = 3.14159265358979323846; 
double e = exp(1);
 
int T,n,m,l,r,d,ans,maxn;
map<int,int>mp;
int a[N],b[N],s[N];
 
void solve(){
      cin >> n >> m;
      if((n+m)%3 == 0 || n % 3 == 0 || m % 3 == 0) cout<<"Possible";
       else cout<<"Impossible";
}
/*
 
*/
signed main(){  
    std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); 
  // cin >> T;
       T = 1;
    while(T--){
        solve();
    } 
    return 0; 
}

问题 F: Snake Toy

题目描述
Snuke has N sticks. The length of the i-th stick is li.
Snuke is making a snake toy by joining K of the sticks together.
The length of the toy is represented by the sum of the individual sticks that compose it. Find the maximum possible length of the toy.

Constraints
1≤K≤N≤50
1≤li≤50
li is an integer.
输入
Input is given from Standard Input in the following format:
N K
l1 l2 l3 … lN
输出
Print the answer.
样例输入 Copy
5 3
1 2 3 4 5
样例输出 Copy
12
提示
You can make a toy of length 12 by joining the sticks of lengths 3, 4 and 5, which is the maximum possible length.

思路:降序排序,累计前k个的值,输出

#pragma GCC optimize(1)
#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
# include<bits/stdc++.h>
# include<unordered_map>
 
# define eps 1e-9
# define fi first
# define se second
# define ll long long
# define int ll
// cout<<fixed<<setprecision(n) 
//bool operator<(const Node& x )
using namespace std;
typedef unsigned long long ull;
typedef pair<int,int > PII; 
const int mod=1e9+7;
const int N=1e6+10;
const int Time=86400;
const int X=131;
const int inf=0x3f3f3f3f;
const double PI = 1e-4;
double pai = 3.14159265358979323846; 
double e = exp(1);
 
int T,n,m,l,r,d,ans,maxn;
map<int,int>mp;
int a[N],b[N],s[N];
 
void solve(){
        cin >> n >> m;
        for(int i = 1 ; i <= n ; i ++ ) cin >> a[i];
        sort(a+1,a+1+n,greater<int>());
        for(int i = 1 ; i <= m ; i++)ans+=a[i];
        cout<<ans;
}
/*
 
*/
signed main(){  
    std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); 
  // cin >> T;
       T = 1;
    while(T--){
        solve();
    } 
    return 0; 
}

问题 G: Splitting Pile

题目描述
Snuke and Raccoon have a heap of N cards. The i-th card from the top has the integer ai written on it.
They will share these cards. First, Snuke will take some number of cards from the top of the heap, then Raccoon will take all the remaining cards. Here, both Snuke and Raccoon have to take at least one card.
Let the sum of the integers on Snuke’s cards and Raccoon’s cards be x and y, respectively. They would like to minimize |x−y|. Find the minimum possible value of |x−y|.

Constraints
2≤N≤2×105
−109≤ai≤109
ai is an integer.
输入
Input is given from Standard Input in the following format:
N
a1 a2 … aN
输出
Print the answer.
样例输入 Copy
6
1 2 3 4 5 6
样例输出 Copy
1
提示
If Snuke takes four cards from the top, and Raccoon takes the remaining two cards, x=10, y=11, and thus |x−y|=1. This is the minimum possible value.

思路:考虑前缀和和后缀和,枚举i然后每次判断前缀和i和后缀和i的abs,更新最大值,这里后缀和可以不用另外开一个数组,直接总和-当前的前缀和即可,注意枚举是1~n-1而不是1 ~ n

#pragma GCC optimize(1)
#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
# include<bits/stdc++.h>
# include<unordered_map>
 
# define eps 1e-9
# define fi first
# define se second
# define ll long long
# define int ll
// cout<<fixed<<setprecision(n) 
//bool operator<(const Node& x )
using namespace std;
typedef unsigned long long ull;
typedef pair<int,int > PII; 
const int mod=1e9+7;
const int N=2e6+10;
const int Time=86400;
const int X=131;
const int inf=0x3f3f3f3f;
const double PI = 1e-4;
double pai = 3.14159265358979323846; 
double e = exp(1);
 
int T,n,m,l,r,d,ans,maxn,sum;
map<int,int>mp;
int a[N],s1[N],s2[N];
void solve(){
        ans = 2e17;
        cin >> n;
        for(int i = 1 ; i <= n ; i ++ ) cin >> a[i],sum+=a[i];
        for(int i = 1 ; i <= n ; i ++ ) s1[i] = s1[i-1] + a[i];
        for(int i = n ; i >= 1 ; i -- ) s2[i] = s2[i+1] + a[i];
         
        for(int i = 1 ; i < n ; i ++ ){
              ans = min(ans,abs(sum-s1[i]-s1[i]));
        }
     
        cout<<ans;
}
/*
 
*/
signed main(){  
    std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); 
  // cin >> T;
       T = 1;
    while(T--){
        solve();
    } 
    return 0; 
}

问题 H: Fennec VS. Snuke

题目描述
Fennec and Snuke are playing a board game.
On the board, there are N cells numbered 1 through N, and N−1 roads, each connecting two cells. Cell ai is adjacent to Cell bi through the i-th road. Every cell can be reached from every other cell by repeatedly traveling to an adjacent cell. In terms of graph theory, the graph formed by the cells and the roads is a tree.
Initially, Cell 1 is painted black, and Cell N is painted white. The other cells are not yet colored. Fennec (who goes first) and Snuke (who goes second) alternately paint an uncolored cell. More specifically, each player performs the following action in her/his turn:
Fennec: selects an uncolored cell that is adjacent to a black cell, and paints it black.
Snuke: selects an uncolored cell that is adjacent to a white cell, and paints it white.
A player loses when she/he cannot paint a cell. Determine the winner of the game when Fennec and Snuke play optimally.

Constraints
2≤N≤105
1≤ai,bi≤N
The given graph is a tree.
输入
Input is given from Standard Input in the following format:
N
a1 b1
:
aN−1 bN−1
输出
If Fennec wins, print Fennec; if Snuke wins, print Snuke.
样例输入 Copy
7
3 6
1 2
3 1
7 4
5 7
1 4
样例输出 Copy
Fennec
提示
For example, if Fennec first paints Cell 2 black, she will win regardless of Snuke’s moves.

思路:考虑BFS,因为题目说了1和n一开始已经分别染了不同白和黑,那么我们就只要看从1能染多少个色,和从n能染多少色,谁多输出对应的哪个人

#pragma GCC optimize(1)
#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
# include<bits/stdc++.h>
# include<unordered_map>
 
# define eps 1e-9
# define fi first
# define se second
# define ll long long
# define int ll
// cout<<fixed<<setprecision(n) 
//bool operator<(const Node& x )
using namespace std;
typedef unsigned long long ull;
typedef pair<int,int > PII; 
const int mod=1e9+7;
const int N=2e6+10;
const int Time=86400;
const int X=131;
const int inf=0x3f3f3f3f;
const double PI = 1e-4;
double pai = 3.14159265358979323846; 
double e = exp(1);
 
int T,n,m,l,r,d,ans,maxn,sum,x,y;
vector<int>g[N];
int vis[N];
int cnt_F,cnt_S;
 
void bfs(){
      queue<int>q;
      q.push(1);
      q.push(n);
      vis[1] = 1 ; vis[n] = -1;
      while(q.size()){
           auto x = q.front();
           q.pop();
           for(auto i : g[x]){
                  int y = i;
                  if(!vis[y]){
                        vis[y] = vis[x];
                        if(vis[y] == -1) cnt_S++;
                         else cnt_F++;
                         
                        q.push(y);
                   }
             }
      }
}
void solve(){
        cin >> n;
        for(int i = 1 ; i < n ; i ++ ){
              int u,v;
              cin >> u >> v;
              g[u].push_back(v);
              g[v].push_back(u);
        }
        bfs();
        cout<<(cnt_F>cnt_S?"Fennec":"Snuke");
}
/*
 
*/
signed main(){  
    std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); 
  // cin >> T;
       T = 1;
    while(T--){
        solve();
    } 
    return 0; 
}

问题 I: Mole and Abandoned Mine

不会写,下次一定。

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值