• [NOI2008]假面舞会——数论+dfs找环


    原题戳这里

    思路

    分三种情况讨论:
    1.有环
    那显然是对于环长取个(gcd)

    2.有类环
    也就是这种情况
    1→2→3→4→5→6→71→8→9→7
    假设第一条链的长度为(l_1),第二条为(l_2),那么(l_1)(l_2)需要满足(l_1equiv l_2(mod k)),也就是(k|(l_1-l_2))。如果我们建权值为(-1)的反向边的话,找出来的环就涵盖了这种情况,并且取(gcd)就能满足等式

    3.有链
    对答案无影响

    最后还需要加一个特判,就是只有链的情况
    具体可以看代码

    #include <algorithm>
    #include  <iostream>
    #include   <cstdlib>
    #include   <cstring>
    #include    <cstdio>
    #include    <string>
    #include    <vector>
    #include     <cmath>
    #include     <ctime>
    #include     <queue>
    #include       <map>
    #include       <set>
    
    using namespace std;
    
    #define IINF 0x3f3f3f3f3f3f3f3fLL
    #define ull unsigned long long
    #define pii pair<int, int>
    #define uint unsigned int
    #define mii map<int, int>
    #define lbd lower_bound
    #define ubd upper_bound
    #define INF 0x3f3f3f3f
    #define vi vector<int>
    #define ll long long
    #define mp make_pair
    #define pb push_back
    
    #define N 100000
    #define M 1000000
    
    struct Edge {
      int next, to, w;
    }e[2*M+5];
    
    int n, m, maxans, minans;
    int fa[N+5], head[N+5], eid, vis[N+5], d[N+5];
    int mmax[N+5], mmin[N+5];
    
    int find(int x) {
      return fa[x] == x ? x : fa[x] = find(fa[x]);
    }
    
    int gcd(int a, int b) {
      return !b ? a : gcd(b, a%b);
    }
    
    void addEdge(int from, int to, int w) {
      e[++eid].next = head[from];
      e[eid].to = to;
      e[eid].w = w;
      head[from] = eid;
    }
    
    void dfs(int u, int x) {
      if(vis[u]) {
        maxans = gcd(abs(x-d[u]), maxans);
        return ;
      }
      vis[u] = 1;
      mmin[find(u)] = min(mmin[find(u)], x);
      mmax[find(u)] = max(mmax[find(u)], x);
      d[u] = x;
      for(int i = head[u]; i; i = e[i].next) {
        int v = e[i].to, w = e[i].w;
        dfs(v, x+w);
      }
    }
    
    int main() {
      scanf("%d%d", &n, &m);
      for(int i = 1; i <= n; ++i) fa[i] = i;
      for(int i = 1, x, y; i <= m; ++i) {
        scanf("%d%d", &x, &y);
        addEdge(x, y, 1), addEdge(y, x, -1);
        int fx = find(x), fy = find(y);
        if(fx != fy) fa[fy] = fx;
      }
      memset(mmin, 0x3f, sizeof mmin);
      for(int i = 1; i <= n; ++i) {
        if(vis[i]) continue;
        dfs(i, 0);
      }
      for(int i = 3; i <= maxans; ++i) {
        if(maxans%i == 0) {
          minans = i;
          break;
        }
      }
      if(!maxans)
        for(int i = 1; i <= n; ++i)
          if(find(i) == i)
            maxans += mmax[i]-mmin[i]+1;
      if(!minans) minans = 3;
      if(maxans < 3) printf("-1 -1
    ");
      else printf("%d %d
    ", maxans, minans);
      return 0;
    }
    
  • 相关阅读:
    c语言的流程控制
    C语言基本用算
    C语言的scanf函数
    C语言一些知识点总结
    C语言程序
    House Robber 分类: leetcode 算法 2015-07-09 20:53 2人阅读 评论(0) 收藏
    [leetcode] Reverse Linked List 分类: leetcode 算法 2015-07-09 18:44 2人阅读 评论(0) 收藏
    合并k个已排序的链表 分类: leetcode 算法 2015-07-09 17:43 3人阅读 评论(0) 收藏
    [leetcode] Reverse Linked List
    Merge k Sorted Lists
  • 原文地址:https://www.cnblogs.com/dummyummy/p/10941877.html
Copyright © 2020-2023  润新知