ABC D - Pair of Balls (模拟+思维+队列)

33 篇文章 0 订阅

链接

题意:

一共有2N个数字,一共N种数字,让你相同的数字匹配,M个桶,只能拿桶首的数字,匹配完的数字拿走。
问你是否能 匹配完。

分析:

无非是模拟栈顶元素的变化的过程。
然后看到使用两两匹配,这样之后我们就可以直接如果这个数字在栈顶出现过两次,那么我们可以直接放进队列中,记录下哪两个桶,用在记录桶下一个元素是哪个。然后循环往复,知道队列为空就好了。
: 用pair时变量比较多,比较乱,那么最好重定变量让自己思路不被复杂的变量打乱。井然有序。这样自己思路清晰,还不容易出BUG。

// Problem: D - Pair of Balls
// Contest: AtCoder - AtCoder Beginner Contest 216
// URL: https://atcoder.jp/contests/abc216/tasks/abc216_d
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef unsigned long long ull;
 
#define x first
#define y second
#define sf scanf
#define pf printf
#define PI acos(-1)
#define inf 0x3f3f3f3f
#define lowbit(x) ((-x)&x)
#define mem(a,x) memset(a,x,sizeof(a))
#define rep(i,n) for(int i=0;i<(n);++i)
#define repi(i,a,b) for(int i=int(a);i<=(b);++i)
#define repr(i,b,a) for(int i=int(b);i>=(a);--i)
#define debug(x) cout << #x << ": " << x << endl;
 
const int MOD = 998244353;
const int mod = 1e9 + 7;
const int maxn = 5e5 + 10;	
const int dx[] = {0, 1, -1, 0, 0};
const int dy[] = {0, 0, 0, 1, -1};
const int dz[] = {1, -1, 0, 0, 0, 0 };
int day[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};


void init(){
	
}
ll n,m;
ll k[maxn];
vector<ll> v[maxn];
ll c[maxn],d[maxn];
queue<pii> q;
void solve()
{
	cin>>n>>m;
	for(int i=1;i<=m;i++){
		scanf("%lld",&k[i]);
		for(int j=0;j<k[i];j++){
			ll x;
			scanf("%lld",&x);
			v[i].push_back(x);
		}
	}
	for(int i=1;i<=m;i++){
		if(k[i]){
			if(c[v[i][0]]){
				q.push({c[v[i][0]],i});	
			}
			c[v[i][0]]=i;
		}
	}
	ll ans=0;
	while(!q.empty()){
		ans++;
		pii top=q.front();
		q.pop();
		
		d[top.x]++;
		ll num =d[top.x],res=top.x;
		if(num<k[res]&&c[v[res][num]]){
			q.push({c[v[res][num]],res});
		}
		else if(num<k[res]){
			c[v[res][num]]=res;
		}
		
		d[top.y]++;
		
		num=d[top.y],res=top.y;
		if(num<k[res]&&c[v[res][num]]){
			q.push({c[v[res][num]],res});
		}
		else if(num<k[res]){
			c[v[res][num]]=res;
		}
	}
	if(ans==n) puts("Yes");
	else puts("No");
	
}
 
int main()
{
	init();
	ll t = 1;
    //scanf("%lld",&t);
    while(t--)
    {
        solve();
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值