这题考查了并查集,只要会模板就能过,我当时有点忘了hh
#include <bits/stdc++.h>
using namespace std;
map<int, vector<int>> mp1;
map<pair<int, int>, int> mp2;
int a[10005];
int N, K, M;
int g(int x)
{
if (x == a[x])
return x;
else
return a[x] = g(a[x]);
}
void me(int x, int y)
{
if (g(x)!=g(y))
{
a[y] = x;
}
}
void init()
{
for(int i=0;i<=N;++i)
{
a[i]=i;
}
}
int main()
{
cin >> N >> M >> K;
init();//一定要初始化,当时写就是忘了初始化
for (int i = 0; i < M; ++i)
{
int x, y, z;
cin >> x >> y >> z;
if (!a[x])
a[x] = x;
if (!a[y])
a[y] = y;
if (z == 1)
{
me(x, y);
}
else
{
mp2[{x, y}] = -1;
mp2[{y, x}] = -1;
}
}
for (int i = 0; i < K; ++i)
{
int x, y;
cin >> x >> y;
if (mp2[{x, y}] == 0&&g(x)==g(y))
{
cout << "No problem" << endl;
}
else if (mp2[{x, y}] == 0 && g(x) != g(y))
{
cout << "OK" << endl;
}
else if (mp2[{x, y}] == -1 && g(x) == g(y))
{
cout << "OK but..." << endl;
}
else
{
cout << "No way" << endl;
}
}
}
#include <bits/stdc++.h>
using namespace std;
long long sum;
struct stu
{
int id;
string zh;
int mo = 0;
bool operator<(const stu a)
{
return this->mo > a.mo || (this->mo == a.mo && this->zh < a.zh);
}
/* data */
} s[10005];
int main()
{
int N, G, K;
cin >> N >> G >> K;
for (int i = 1; i <= N; ++i)
{
cin >> s[i].zh >> s[i].mo;
}
sort(s + 1, s + N + 1);
int temp = 1;
s[0].mo=-1;
for (int i = 1; i <= N; ++i)
{
if(i>=2&&s[i].mo==s[i-1].mo){
s[i].id=s[i-1].id;
}
else {
s[i].id=i;
}
if (s[i].mo >= G&&s[i].mo<=100)
{
sum += 50;
}
else if (s[i].mo < G && s[i].mo >= 60)
{
sum += 20;
}
}
cout << sum << endl;
for(int i=1;i<=N;++i)//for循环条件不能写s[i].id<=K,因为如果全部并列的话,会一直循环下去,产生段错误
{
if(s[i].id<=K)
cout<<s[i].id<<" "<<s[i].zh<<" "<<s[i].mo<<endl;
else break;
// printf("%d %s %d\n",s[i].id, s[i].zh.c_str(), s[i].mo);
}
}
这题就是一个简单模拟,跟着题目意思走就行
#include <bits/stdc++.h>
using namespace std;
int N, M, Sm;
queue<char> que[1005];
stack<char> sta;
int main()
{
cin >> N >> M >> Sm;
for (int i = 1; i <= N; ++i)
{
string s;
cin >> s;
for (int j = 0; j < s.length(); ++j)
{
que[i].push(s[j]);
}
}
while (1)
{
int o;
cin >> o;
if (o == -1)
{
break;
}
else if (o == 0)
{
if ( sta.empty())
{
continue;
}
else
{
cout << sta.top();
sta.pop();
}
}
else
{
if(que[o].empty()){
continue;
}
if (sta.size() < Sm)
{
sta.push(que[o].front());
que[o].pop();
}
else
{
cout << sta.top();
sta.pop();
sta.push(que[o].front());
que[o].pop();
}
}
}
}
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+5;
struct node
{
int sex;//关于性别,1表示M,2表示F
int fa;//初始值全为-1.设为父母不可考
int mo;
/* data */
}Node[maxn];//家族人员信息数组
int flag;//全局变量标注是否出五服
int cnt[maxn];//五服存储
//遍历以id为根节点的家族树,并记录相应编号已被遍历过,num为层数
void Find(int id,int num)
{
if(num==4)return;//已经到达五代祖先
if(Node[id].fa!=-1)//有父亲
{
if(cnt[Node[id].fa]==1)//如果父亲编号已经在之前被遍历过了
{
flag=1;//标记为出五服了
return;
}
cnt[Node[id].fa]=1;//标记为已经遍历过了
Find(Node[id].fa,num+1);//继续向上遍历
}
if(Node[id].mo!=-1)
{
if(cnt[Node[id].mo]==1)
{
flag=1;
return;
}
cnt[Node[id].mo]=1;
Find(Node[id].mo,num+1);
}
}
int main()
{
int n;
cin>>n;
//初始化家族人员信息数组
for(int i=0;i<maxn;++i){
Node[i].fa=Node[i].mo=-1;
}
//读入家族人员信息
for(int i=0;i<n;++i)
{
int id,father,mother;
char x;
scanf("%d %c %d %d",&id,&x,&father,&mother);
if(x=='M')Node[id].sex=1;
else if(x=='F')Node[id].sex=2;
Node[id].fa=father;
Node[id].mo=mother;
//父母的相应编号,需要标性别
if(father!=-1)Node[father].sex=1;
if(mother!=-1)Node[mother].sex=2;
}
int k;
scanf("%d",&k);
while(k--)
{
int n1,n2;
scanf("%d %d",&n1,&n2);
if(Node[n1].sex==Node[n2].sex)printf("Never Mind\n");
else{
//清零数组及标记
memset(cnt,0,sizeof(cnt));
flag=0;
Find(n1,0);
Find(n2,0);
if(flag)printf("No\n");
else printf("Yes\n");
}
}
}