#20293: 求救:TLE(60%)


089487 (089487)

學校 : 國立臺灣師範大學附屬高級中學
編號 : 82069
來源 : [220.130.10.185]
最後登入時間 :
2024-04-01 11:16:18
d808. 黑暗部落 | From: [111.71.52.63] | 發表日期 : 2019-12-25 19:36

請問我的程式要如何修正

#include<bits/stdc++.h>
using namespace std;
unordered_set<int> st[1000001];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int t;
while(cin>>t)
{

for(int i=1;i<=t;i++)
{
st[i].insert(i);
int k;
cin>>k;
st[i].insert(k);
st[k].insert(i);
}
int max=-1;
int length=0;
for(int i=1;i<=t;i++)
{
queue<int> q;
for(auto j:st[i]) q.push(j);
// cout<<"q";
//q.push(-1);
while(!q.empty())
{
if(q.front()==i) {q.pop();continue;
}
// cout<<q.front()<<"q\n";
for(auto j:st[q.front()])
{
if(st[i].find(j)==st[i].end())
{
q.push(j);
st[i].insert(j);
}
}
st[q.front()].clear();
q.pop();
//if() break;
}
// cout<<"st:";
//for(auto j:st[i]) cout<<j<<" ";
//cout<<"\n";
int a=st[i].size();
if(a>max) max=a;
//cout<<st[i].size()<<" "<<max<<" ";
if(st[i].size()) length++;
st[i].clear();
}
cout<<length<<" "<<max<<"\n";
}
}

 
ZeroJudge Forum