HHKB Programming Contest 2023(AtCoder Beginner Contest 327)
A √
输出是否存在相邻的字母 ab
B ×
求立方, 不要用 pow
函数, 一定要自己写
快速幂
ll ksm(ll a, ll b, ll p){
ll res=1;
a%=p;
while(b){
if(b&1) res=res*a%p;
b>>=1;
a=a*a%p;
}
return res;
}
C √
纯粹的模拟
D ×
判断是否存在奇环, 即判断是否是二分图
#include<bits/stdc++.h>
using namespace std;
int n, m;
int const N = 2e5+10;
int h[N], e[N<<1], ne[N<<1], idx;
int a[N], b[N], color[N];
void add(int a, int b){
e[idx]=b, ne[idx]=h[a], h[a]=idx++;
}
bool dfs(int u, int c){
color[u]=c;
for(int i=h[u]; ~i; i=ne[i]){
int j=e[i];
if(!color[j]){
if(!dfs(j,3-c)) return false;
}
else if(color[j]==c) return false;
}
return true;
}
bool solve(){
memset(h, -1, sizeof h);
cin>>n>>m;
for(int i=1; i<=m; i++) cin>>a[i];
for(int i=1; i<=m; i++) cin>>b[i];
for(int i=1; i<=m; i++){
add(a[i],b[i]); add(b[i], a[i]);
}
for(int i=1; i<=n; i++){
if(!color[i]){
if(!dfs(i,1)){
return false;
}
}
}
return true;
}
int main(){
ios::sync_with_stdio(false); cin.tie(0), cout.tie(0);
cout << (solve() ? "Yes" : "No") << endl;
return 0;
}