Problem : Link. I simply did bfs from node 1 and found the maximum distance from this node to all other nodes
Code
So the solution is O(n+m) but still, there is TLE in one case. Why?
# | User | Rating |
---|---|---|
1 | tourist | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 170 |
2 | Um_nik | 162 |
3 | maomao90 | 161 |
4 | atcoder_official | 160 |
5 | djm03178 | 158 |
5 | -is-this-fft- | 158 |
7 | adamant | 154 |
7 | Dominater069 | 154 |
9 | awoo | 152 |
9 | luogu_official | 152 |
CSES Longest Flight Route TLE in one test case
Problem : Link. I simply did bfs from node 1 and found the maximum distance from this node to all other nodes
const int INF = 1e9;
const int sz = 1e5+1;
int n,m;
vector<int> last(sz);
vector<vector<int> > adj(sz);
vector<int> d(sz,0);
void solve()
{
cin>>n>>m;
for(int i=0; i<m; ++i)
{
int a,b;
cin>>a>>b;
adj[a].push_back(b);
}
queue<int> q;
q.push(1);
while(!q.empty())
{
int s = q.front();
q.pop();
for(auto u : adj[s])
{
if(d[s]+1>d[u])
{
d[u]=1+d[s];
last[u]=s;
q.push(u);
}
}
}
if(d[n]==0)
{
cout<<"IMPOSSIBLE"<<endl;
return;
}
vector<int> ans;
int k=n;
while(k!=1)
{
ans.push_back(k);
k=last[k];
}
ans.push_back(1);
reverse(ans.begin(), ans.end());
cout<<1+d[n]<<endl;
for(int i=0; i<ans.size(); ++i)
cout<<ans[i]<<" ";
cout<<endl;
}
int main()
{
solve();
}
So the solution is O(n+m) but still, there is TLE in one case. Why?
Rev. | Lang. | By | When | Δ | Comment | |
---|---|---|---|---|---|---|
en5 | salt_n_ice | 2020-12-03 13:46:13 | 56 | |||
en4 | salt_n_ice | 2020-12-03 13:45:53 | 62 | Tiny change: 'Problem : ' -> '<spoiler summary="Spoiler">\n...\n..testing\n</spoiler>\nProblem : ' | ||
en3 | salt_n_ice | 2020-12-03 13:41:51 | 16 | |||
en2 | salt_n_ice | 2020-12-03 13:40:55 | 73 | |||
en1 | salt_n_ice | 2020-12-03 13:39:52 | 1329 | Initial revision (published) |
Name |
---|