跳到正文

题解

「YLLOI-R1-T3」一路向北 题解

XsIeEiKcEk约 2 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 2 分钟
题目
P12414

版本

  1. v1当前

我的思路

将这道题看作一个模拟题做,再来一些剪枝,节省时间。

CODE

C++
#include<iostream>
#include<queue>
using namespace std;
int main(){
    int t;
    cin>>t;
    while(t--){
        int n,m;
        queue<int>q[100000];
        cin>>n>>m;
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                int c;
                cin>>c;
                q[i].push(c);
            }
        }
        for(int i=0;i<n;i++){//模拟将0放入的队列
            q[i].push(0);
            int now=q[i].front(),c=1;//c为次数,后有用,更新now值
            q[i].pop();
            while(now!=0&&c<=n*m){//若c>n*m,代表所有数都以遍历,跳出
                q[now-1].push(now);//更新
                int _n=q[now-1].front();
                q[now-1].pop();
                now=_n;
                c++;
            }
            if(now!=0){//若不为0,直接输出
                cout<<"Yes\n";
                break;
            }
            if(i==n-1)cout<<"No\n";
        }
    }
    return 0;
}
原文