比赛 2025新春开学欢乐赛 评测结果 AAATTTTTTT
题目名称 t1 最终得分 30
用户昵称 徐诗畅 运行时间 14.141 s
代码语言 C++ 内存使用 7.41 MiB
提交时间 2025-02-15 17:13:18
显示代码纯文本
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+5;
int n,ans[N];
unordered_map<int,int> mp;
int main(){
	freopen("flurryofblows.in","r",stdin);
	freopen("flurryofblows.out","w",stdout);
	for(int i = 0;i<=1e6;i++) ans[i]=-1;
    scanf("%d",&n);
    mp[1] = 0;
    for(int i = 0;i<n;i++){
        int type; cin>>type;
        if(type==1){
            int x; scanf("%d",&x);
            if(mp.empty()) continue;
            int mi = 1e9;
            for(const auto& p:mp){
                if(p.second<mi) 
				mi = p.second;
            }
            unordered_map<int,int> nmp; nmp[x] = mi;
            for(const auto& p:mp){
                int st=p.first,c=p.second+1;
                auto it=nmp.find(st);
                if(it==nmp.end()) nmp[st]=c;
                else if(c<it->second) it->second=c;
            }
            mp.swap(nmp);
        }
		else{
            int x,y; scanf("%d%d",&x,&y);
            unordered_map<int,int> nmp;
            for(const auto& p : mp){
                int st = p.first,c = p.second;
                int ns =(st == x)?y:st;
                auto it = nmp.find(ns);
                if(it == nmp.end()){
                    nmp[ns] = c;
                } 
				else if(c<it->second) it->second = c;
            }
            for(const auto& p : mp){
                int st = p.first;
                int c = p.second + 1;
                auto it = nmp.find(st);
                if(it == nmp.end()){
                    nmp[st] = c;
                }
                else if(c<it->second) it->second = c;
            }
            mp.swap(nmp);
        }
    }
    for(const auto& p : mp){
        int st = p.first;
        if(st>=1&&st<=n){
            if(ans[st]==-1||p.second<ans[st]){
                ans[st]=p.second;
            }
        }
    }
    for(int i = 1;i<=n;i++)
    printf("%d ",ans[i]);
    return 0;
}