简体   繁体   English

为什么我的 C++14 KosaRaju 算法在类似的编写代码运行得更快时得到 TLE

[英]Why my C++14 KosaRaju algo getting TLE when a similar written code runs much faster

TLE code completes at 2.1 secs. TLE 代码在 2.1 秒内完成。 I'm also passing many things through reference but it's still throwing a TLE.我也通过引用传递了很多东西,但它仍然抛出一个 TLE。 Why this code takes so much time?为什么这段代码要花这么多时间?

here is the problem at hackerearth:这是hackerearth的问题:

https://www.hackerearth.com/problem/algorithm/falling-dominos-49b1ed46/ https://www.hackerearth.com/problem/algorithm/falling-dominos-49b1ed46/

Dominos are lots of fun.多米诺骨牌很有趣。 Children like to stand the tiles on their side in long lines.孩子们喜欢把瓷砖排成一排排在他们身边。 When one domino falls, it knocks down the next one, which knocks down the one after that, all the way down the line.当一个多米诺骨牌倒下时,它会击倒下一个,下一个又会击倒下一个,一直向下。 However, sometimes a domino fails to knock the next one down.然而,有时一张多米诺骨牌无法击倒下一张。 In that case, we have to knock it down by hand to get the dominos falling again.那样的话,我们就得用手把它推倒,才能让多米诺骨牌再次倒下。 Your task is to determine, given the layout of some domino tiles, the minimum number of dominos that must be knocked down by hand in order for all of the dominos to fall.您的任务是根据一些多米诺骨牌的布局,确定必须用手推倒的最少多米诺骨牌数量才能使所有多米诺骨牌倒下。

Input输入

The first line of input contains one integer specifying the number of test cases to follow.输入的第一行包含一个 integer 指定要遵循的测试用例数。 Each test case begins with a line containing two integers, each no larger than 100 000. The first integer n is the number of domino tiles and the second integer m is the number of lines to follow in the test case.每个测试用例都以一行包含两个整数开始,每个整数不大于 100 000。第一个 integer n 是多米诺骨牌的数量,第二个 integer m 是测试用例中要遵循的行数。 The domino tiles are numbered from 1 to n.多米诺骨牌从 1 到 n 编号。 Each of the following lines contains two integers x and y indicating that if domino number x falls, it will cause domino number y to fall as well.以下每一行包含两个整数 x 和 y,表示如果多米诺骨牌 x 倒下,它会导致多米诺骨牌 y 也倒下。

Output Output

For each test case, output a line containing one integer, the minimum number of dominos that must be knocked over by hand in order for all the dominos to fall.对于每个测试用例,output 包含一个 integer 的一行,这是为了让所有多米诺骨牌都倒下而必须用手推倒的最少多米诺骨牌数。

SAMPLE INPUT 1 3 2 1 2 2 3 SAMPLE OUTPUT 1样本输入 1 3 2 1 2 2 3 样本 OUTPUT 1

code completes at 2.1代码在 2.1 完成

#include <iostream>
    #include <vector>
    #include <unordered_set>
    #include <stack>

    using namespace std;


    void dfs(const vector<vector<int>> &edges, unordered_set<int> &visited,int sv, stack<int> &stk){
        visited.insert(sv);
        for(int i=0;i<edges[sv].size();i++){
            int current = edges[sv][i];
            if(visited.find(current)==visited.end())
                dfs(edges, visited, current, stk);
        }
        stk.push(sv);
    }

    void dfs(const vector<vector<int>> &edges, unordered_set<int> &visited,int sv){
        visited.insert(sv);
        for(int i=0;i<edges[sv].size();i++){
            int current = edges[sv][i];
            if(visited.find(current)==visited.end())
                dfs(edges, visited, current);
        }
    }


    int main()
    {
        int t;
        cin>>t;
        while(t--){
            int V, E;
            cin>>V>>E;
            vector<vector<int>> edges(V+1);
            unordered_set<int> visited;
            stack<int> stk;
            while(E--){
                int f, s;
                cin>>f>>s;
                edges[f].push_back(s);
            }

            for(int i=1;i<=V;i++)
                if(visited.find(i)==visited.end())
                    dfs(edges, visited, i, stk);

            visited.clear();
            int count{0};
            while(!stk.empty()){
                int current = stk.top();
                stk.pop();
                if(visited.find(current)==visited.end()){
                dfs(edges, visited, current);
                count++;
                }
            }
           cout<<count<<endl;
        }
        return 0;
    }

Efficient Code completes at 0.7 sec.高效代码在 0.7 秒内完成。

  #include<iostream>

    #include<bits/stdc++.h>
    using namespace std;

       void dfs( vector<int> *edges , int start,int n,bool *visit ,stack<int> *nodex)
        {

          visit[start]  = true;
    //       cout<<start<<endl;

          for (int i = 0; i < edges[start].size(); ++i)
          {
                int next = edges[start][i];

                  if(visit[next] == false)
                   dfs(edges,next,n,visit,nodex);

          }

             nodex->push(start);
        }

     void dfs(vector<int> *edges,int start, bool *visit,int n)
    {
        visit[start] = true;

        for(int i=0;i<edges[start].size();i++)
        {
        int next = edges[start][i]; 
            if(visit[next]==false)
            dfs(edges,next,visit,n);
        }
    }

    int main()
    {
        int t;
        cin>>t;
      while(t--)
    {
           int n,m;
           cin>>n>>m;

           vector<int> *edges = new vector<int>[n+1];

                for (int i = 0; i < m; ++i)
                {
                    int start,end;
                     cin>>start>>end;

                     edges[start].push_back(end);  
                }

                //  cout<<"PHASE 1"<<endl;

                  bool *visit = new bool[n+1];

                  for (int i = 0; i<=n; ++i)
                  {
                    visit[i] = false;
                  }


                stack<int> *nodex = new stack<int>();

                 for (int i = 1; i<=n; ++i)
                   {
                     if(visit[i]  == false)
                       dfs(edges,i,n,visit,nodex);
                   }
                //   cout<<"PHASE 2"<<endl;

             for(int i=0;i<=n;i++)
              visit[i] = false;

                   int count=0;
                   while(!nodex->empty())
                        {
                       int up = nodex->top();
                        nodex->pop();
    //                  cout<<" EVERYTHING ISS FINE  "<<up<<endl;
                            if(visit[up] ==false )
                            {
                                dfs(edges,up,visit,n);
                                count++;
                            }       
                    //        cout<<"Everrything is fine "<<up<<endl;

                        }
                        cout<<count<<endl;

    }

        return 0;
    }

Use Fast I/O and "\n"in place of endl.使用快速 I/O 和 "\n" 代替 endl。 This helps a lot in getting rid of TLE.这对摆脱 TLE 有很大帮助。 For me the rest of the code seems to be fine对我来说,代码的 rest 似乎没问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM