Problem : Link. I simply did bfs from node 1 and found the maximum distance from this node to all other nodes
My solution
So the solution is O(n+m) but still, there is TLE in one case. Why?
# | User | Rating |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3831 |
3 | Radewoosh | 3646 |
4 | jqdai0815 | 3620 |
4 | Benq | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | gamegame | 3386 |
10 | ksun48 | 3373 |
# | User | Contrib. |
---|---|---|
1 | cry | 164 |
1 | maomao90 | 164 |
3 | Um_nik | 163 |
4 | atcoder_official | 160 |
5 | -is-this-fft- | 158 |
6 | awoo | 157 |
7 | adamant | 156 |
8 | TheScrasse | 154 |
8 | nor | 154 |
10 | Dominater069 | 153 |
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 |
---|