2 条题解

  • 0
    @ 2025-10-8 16:58:15
    #include <bits/stdc++.h>
    using namespace std;
    const int N = 1e6 + 10;
    struct edge {
        int x, y, pre, m;
    } a[N];
    int alen, last[N];
    int n, m1, m2, rd[N], q[N], qlen1, qlen2;
    
    void ins(int x, int y, int m) {
        alen++;
        a[alen] = {x, y, last[x], m};
        last[x] = alen;
    }
    
    int main() {
        scanf("%d%d%d", &n, &m1, &m2);
        alen = 0;
        memset(last, 0, sizeof(last));
        for (int i = 1; i <= m1; i++) {
            int x, y;
            scanf("%d%d", &x, &y);
            ins(x, y, 0);
            rd[y]++;
        }
    
        if (alen % 2 == 0) alen++;
    
        for (int i = 1; i <= m2; i++) {
            int x, y;
            scanf("%d%d", &x, &y);
            ins(x, y, 1);
            ins(y, x, 1);
        }
    
        queue<int> q;
        for (int i = 1; i <= n; i++)
            if (rd[i] == 0) q.push(i);
    
        while (!q.empty()) {
            int x = q.front();
            q.pop();
            for (int i = last[x]; i; i = a[i].pre)
                if (a[i].m == 0) {
                    rd[a[i].y]--;
                    if (rd[a[i].y] == 0) q.push(a[i].y);
                }
            for (int i = last[x]; i; i = a[i].pre)
                if (a[i].m == 1) {
                    a[i ^ 1].m = 2; // 废掉反向边
                }
        }
    
        for (int i = 1; i <= alen; i++)
            if (a[i].m == 1) {
                // cout << a[i].m;
                printf("%d %d\n", a[i].x, a[i].y);
            }
    
        return 0;
    }
    
    • 0
      @ 2025-10-8 16:58:09
      #include<bits/stdc++.h>
      using namespace std;
      const int N=1e6+10;
      struct edge
      {
          int x,y,pre,m;
      }a[N];int alen,last[N];
      int n,m1,m2,rd[N],q[N],qlen1,qlen2;
      void ins(int x,int y,int m)
      {
          alen++;a[alen]={x,y,last[x],m};last[x]=alen;
      }
      int main()
      {
          scanf("%d%d%d",&n,&m1,&m2);
          alen=0;memset(last,0,sizeof(last));
          for(int i=1;i<=m1;i++)
          {
              int x,y;scanf("%d%d",&x,&y);
              ins(x,y,0);rd[y]++;
          }
      
          if(alen%2==0)alen++;
      
          for(int i=1;i<=m2;i++)
          {
              int x,y;scanf("%d%d",&x,&y);
              ins(x,y,1);ins(y,x,1);
          }
          queue<int> q;
          for(int i=1;i<=n;i++)if(rd[i]==0)q.push(i); 
      
          while(!q.empty())
          {
              int x=q.front();q.pop();
              for(int i=last[x];i;i=a[i].pre)if(a[i].m==0)
              {
                  rd[a[i].y]--;
                  if(rd[a[i].y]==0)q.push(a[i].y);
              }
              for(int i=last[x];i;i=a[i].pre)if(a[i].m==1)
              {
                  a[i^1].m=2;//废掉反向边
              }
          }
           
          for(int i=1;i<=alen;i++)if(a[i].m==1)
          {
              //cout<<a[i].m;
              printf("%d %d\n",a[i].x,a[i].y);
          }
           
          return 0;
      }
      • 1

      【拓扑】混合图无环[USACO09DEC] Dizzy Cows G(spj)

      信息

      ID
      1732
      时间
      1000ms
      内存
      128MiB
      难度
      10
      标签
      递交数
      56
      已通过
      2
      上传者