PAT (Advanced Level) 1013 并查集求连通分量个数 1014 模拟(有坑,在规定时间未处理完的任务要处理完) 1035(简单题)

PAT 1013

#include<bits/stdc++.h>
using namespace std;
const int maxn=1000+5;
int parent[maxn];
int find(int x)
{
	return x==parent[x]?x:parent[x]=find(parent[x]);
}
int v,e;
void init()
{
	for(int i=1;i<=v;i++)
		parent[i]=i;
}
int main()
{
	vector<int>G[maxn];
	scanf("%d%d",&v,&e);
	for(int i=1;i<=v;i++)
		G[i].clear();
	int query;
	scanf("%d",&query);
	int maxv=-1;
	for(int i=0;i<e;i++)
	{
		int v1,v2;
		scanf("%d%d",&v1,&v2);
		G[v1].push_back(v2);
		G[v2].push_back(v1);
	}
	while(query--)
	{
		init();
		int k;
		scanf("%d",&k);
		for(int i=1;i<=v;i++)
		{
			if(i==k) continue;
			for(int j=0;j<G[i].size();j++)
			{
				if(G[i][j]==k) continue;
				int p1=find(i);
				int p2=find(G[i][j]);
				if(p1!=p2) parent[p1]=p2;
			}
		}
		int num=0;
		for(int i=1;i<=v;i++)
		{
			int p1=find(i);
			if(p1==i) num++;
		}
		printf("%d\n",num-2);
	}
	return 0;
} 

PAT 1014

模拟

//模拟
#include<bits/stdc++.h>
using namespace std;
const int maxn=1000+5;
int main()
{
    int n,m,k,q;
    scanf("%d%d%d%d",&n,&m,&k,&q);
    int time[maxn];
    for(int i=1;i<=k;i++)
        scanf("%d",&time[i]);
    //预处理
    int person[maxn];
    memset(person,-1,sizeof(person));
    queue<int>G[maxn];
    for(int i=1;i<=n;i++)
    {
        while(!G[i].empty()) G[i].pop();
    }
    int wait=n*m+1,pos=0;
    for(int i=1;i<=n*m;i++)
    {
        if(i>k) {
                wait=-1;
                break;
        }
        G[pos%n+1].push(i);
        pos++;
    }
    int add[maxn];
    memset(add,0,sizeof(add));
    for(int i=1;i<=540;i++)                     //the time later
    {
        for(int j=1;j<=n;j++)
        {
            if(!G[j].empty())
            {
                pos=G[j].front();
                person[pos]=add[j];                     //记录开始时间
                if(time[pos]+add[j]-i==0) {
                    add[j]+=time[pos];
                    G[j].pop();
                    if(wait!=-1&&i<540) {
                        G[j].push(wait++);
                        if(wait>k) wait=-1;
                    }
                }
            }
        }
    }
    while(q--)
    {
        scanf("%d",&pos);
        int x=person[pos];
        if(x==-1) printf("Sorry\n");
        else {
            printf("%02d:%02d\n",8+(x+time[pos])/60,(x+time[pos])%60);
        }
    }
    return 0;
}

PAT 1035

#include<bits/stdc++.h>
using namespace std;
typedef pair<string,string>PII;
const int maxn=10+5;
int main()
{
    int n,flag=0;
    scanf("%d",&n);
    PII str[1005];
    for(int i=0;i<n;i++)
    {
        bool done=false;
        char s1[maxn],s2[maxn];
        scanf("%s%s",s1,s2);
        for(int j=0;j<strlen(s2);j++)
        {
            if(s2[j]=='0'||s2[j]=='1'||s2[j]=='l'||s2[j]=='O') {
                done=true;
                if(s2[j]=='0') {
                    s2[j]='%';
                }
                if(s2[j]=='1') {
                    s2[j]='@';
                }
                if(s2[j]=='l') {
                    s2[j]='L';
                }
                if(s2[j]=='O') {
                    s2[j]='o';
                }
            }
        }
        if(done)
        {
            flag++;
            str[flag].first=string(s1);
            str[flag].second=string(s2);
        }
    }
    if(flag==0) {
        if(n==1) {
            printf("There is 1 account and no account is modified\n");
        }
        else {
            printf("There are %d accounts and no account is modified\n",n);
        }
    }
   else {
        cout<<flag<<endl;
        for(int i=1;i<=flag;i++)
            cout<<str[i].first<<" "<<str[i].second<<endl;
   }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值