Connections Gym - 101630C DFS/有向图的强联通分量边集

题目链接

求有向图强连通分量的2n个边的边集
首先从1开始DFS可以走到的所有点,并记录走过的边
这样就满足了从1可达所有其他点
然后从1开始DFS走反向边,这样保证其他点可达1点.
两遍dfs       
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5+5;


struct node
{
    int to;
    int ind;
};



int u[maxn];
int v[maxn];
int ans[maxn];

vector<node> G[maxn];
vector<node> G1[maxn];

int T;
int n,m;
int vis[maxn];

void dfs(int cur)
{
    vis[cur]=1;
    for(int i=0;i<G[cur].size();i++)
    {
        if(vis[G[cur][i].to ]) continue;
        ans[ G[cur][i].ind ] = 1;
        dfs(G[cur][i].to );
    }
}


void dfs1(int cur)
{
    vis[cur]=1;
    for(int i=0;i<G1[cur].size();i++)
    {
        if(vis[G1[cur][i].to ]) continue;
        ans[ G1[cur][i].ind ] = 1;
        dfs1(G1[cur][i].to );
    }
}



int main()
{
//    freopen("data.txt","r",stdin);
//    ios_base::sync_with_stdio(false);
//    cin >> T;
    scanf("%d",&T);
    while(T--)
    {
        memset(ans,0,sizeof(ans));
        memset(vis,0,sizeof(vis));
        scanf("%d %d",&n,&m);

        for(int i=0;i<=n;i++)
        {
            G[i].clear();
        }
        for(int i=0;i<=n;i++)
        {
            G1[i].clear();
        }

        node nt;
        for(int i=1;i<=m;i++)
        {

            scanf("%d %d",&u[i],&v[i]);
            nt.to = v[i];
            nt.ind = i;
            G[u[i]].push_back(nt);


            nt.to = u[i];
            nt.ind = i;
            G1[v[i]].push_back(nt);

        }

        dfs(1);


        memset(vis,0,sizeof(vis));
        dfs1(1);

        int ct = m-2*n;

        for(int i=1;i<=m&&ct;i++)
        {
            if(!ans[i])
            {
                ct--;
                printf("%d %d\n",u[i],v[i]);
            }
        }


    }

    return 0;
}












  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Traceback (most recent call last): File "/usr/local/python3/lib/python3.9/site-packages/pymysql/connections.py", line 732, in _read_bytes data = self._rfile.read(num_bytes) File "/usr/local/python3/lib/python3.9/socket.py", line 704, in readinto return self._sock.recv_into(b) socket.timeout: timed out During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/usr/local/datax-web/modules/datax-executor/bin/../data/applogs/executor/jobhandler/gluesource/833_1678761378000.py", line 36, in <module> db.execute("REPLACE INTO datax_customer_basc_detail_opt " File "/usr/local/python3/lib/python3.9/site-packages/pymysql/cursors.py", line 148, in execute result = self._query(query) File "/usr/local/python3/lib/python3.9/site-packages/pymysql/cursors.py", line 310, in _query conn.query(q) File "/usr/local/python3/lib/python3.9/site-packages/pymysql/connections.py", line 548, in query self._affected_rows = self._read_query_result(unbuffered=unbuffered) File "/usr/local/python3/lib/python3.9/site-packages/pymysql/connections.py", line 775, in _read_query_result result.read() File "/usr/local/python3/lib/python3.9/site-packages/pymysql/connections.py", line 1156, in read first_packet = self.connection._read_packet() File "/usr/local/python3/lib/python3.9/site-packages/pymysql/connections.py", line 692, in _read_packet packet_header = self._read_bytes(4) File "/usr/local/python3/lib/python3.9/site-packages/pymysql/connections.py", line 738, in _read_bytes raise err.OperationalError( pymysql.err.OperationalError: (2013, 'Lost connection to MySQL server during query (timed out)') During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/usr/local/datax-web/modules/datax-executor/bin/../data/applogs/executor/jobhandler/gluesource/833_1678761378000.py", line 66, in <module> six.reraise(exc) TypeError: reraise() missing 1 required positional argument: 'value'
最新发布
07-12

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值