• 22/6/30


    1,acwing 164.可达性统计;2, Decorating the Pastures;3, Perimeter


    1,可达性统计; 

     思路:设f[x]表示x能到达的点的个数,

    那么f[x]=(x)||f(y1)||f(y2)||....   (’||‘就是或运算,y1,y2是存在有向边的(x,y1)...);

    因此,要求f[x]就要知道后面的f[y1],f[y2]..所以就要按拓扑排序的逆序来做;

    在表示x能否到i,可以用状态压缩,但是数据很长不是32位,就用到了bitset;

    也就是将状态压缩不局限int来表示,而是可定长度的01数组来表示,并且 支持位运算;

    一般来说是bitset<N>f[N],f[i]代表一串01字符(一行),第i位是1就表示能到;

    f[i][i]就是本身;

    1. #pragma GCC optimize(2)
    2. #include<bits/stdc++.h>
    3. #define rep1(i,a,n) for( int i=a;i<n;++i)
    4. #define rep2(i,a,n) for( int i=a;i<=n;++i)
    5. #define per1(i,n,a) for( int i=n;i>a;i--)
    6. #define per2(i,n,a) for( int i=n;i>=a;i--)
    7. #define quick_cin() cin.tie(0),cout.tie(0),ios::sync_with_stdio(false)
    8. #define memset(a,i,b) memset((a),(i),sizeof (b))
    9. #define memcpy(a,i,b) memcpy((a),(i),sizeof (b))
    10. #define pro_q priority_queue
    11. #define pb push_back
    12. #define pf push_front
    13. #define endl "\n"
    14. #define lowbit(m) ((-m)&(m))
    15. #define YES cout<<"YES\n"
    16. #define NO cout<<"NO\n"
    17. #define Yes cout<<"Yes\n"
    18. #define No cout<<"No\n"
    19. #define yes cout<<"yes\n"
    20. #define no cout<<"no\n"
    21. #define yi first
    22. #define er second
    23. using namespace std;
    24. typedef pair<long long,long long>PLL;
    25. typedef long long LL;
    26. typedef pair<int,int> PII;
    27. typedef pair<int,PII> PIII;
    28. typedef double dob;
    29. const int N=3e4+10;
    30. int e[N],ne[N],h[N],idx;
    31. int d[N];
    32. void add(int a,int b)
    33. {
    34. e[idx]=b,ne[idx]=h[a],h[a]=idx++;
    35. }
    36. int n,m;
    37. vector<int>xl;
    38. bitset<N>f[N];
    39. void topsort()
    40. {
    41. queue<int>q;
    42. rep2(i,1,n)if(!d[i])q.push(i);
    43. while (q.size())
    44. {
    45. int t=q.front();
    46. q.pop();
    47. xl.pb(t);
    48. for(int i=h[t];~i;i=ne[i])
    49. {
    50. int j=e[i];
    51. if(--d[j]==0)
    52. {
    53. q.push(j);
    54. }
    55. }
    56. }
    57. }
    58. signed main()
    59. {
    60. quick_cin();
    61. cin>>n>>m;
    62. memset(h,-1,h);
    63. while(m--)
    64. {
    65. int a,b;
    66. cin>>a>>b;
    67. add(a,b);
    68. d[b]++;
    69. }
    70. topsort();
    71. per2(i,n-1,0)
    72. {
    73. int j=xl[i];
    74. f[j][j]=1;
    75. for(int k=h[j];~k;k=ne[k])
    76. {
    77. f[j]|=f[e[k]];
    78. }
    79. }
    80. rep2(i,1,n)cout<<f[i].count()<<endl;
    81. return 0;
    82. }

    2, Decorating the Pastures

    题意:

    n个牧场,m条双向道路连接,现给每个牧场插旗,有F和J两种,规定一条道路连接的两个牧场不能插同一种,求J最大的数,如果没有输出-1;

    思路:不知道有没有孤立点,就遍历每个牧场,遍历过的标记,然后开始插旗,差的时候下一层和父亲不能一样,判断没有方案就是父亲和孩子插的旗一样;

    1. #pragma GCC optimize(2)
    2. #include<bits/stdc++.h>
    3. #define rep1(i,a,n) for( int i=a;i<n;++i)
    4. #define rep2(i,a,n) for( int i=a;i<=n;++i)
    5. #define per1(i,n,a) for( int i=n;i>a;i--)
    6. #define per2(i,n,a) for( int i=n;i>=a;i--)
    7. #define quick_cin() cin.tie(0),cout.tie(0),ios::sync_with_stdio(false)
    8. #define memset(a,i,b) memset((a),(i),sizeof (b))
    9. #define memcpy(a,i,b) memcpy((a),(i),sizeof (b))
    10. #define pro_q priority_queue
    11. #define pb push_back
    12. #define pf push_front
    13. #define endl "\n"
    14. #define lowbit(m) ((-m)&(m))
    15. #define YES cout<<"YES\n"
    16. #define NO cout<<"NO\n"
    17. #define Yes cout<<"Yes\n"
    18. #define No cout<<"No\n"
    19. #define yes cout<<"yes\n"
    20. #define no cout<<"no\n"
    21. #define yi first
    22. #define er second
    23. #define INF 0x3f3f3f3f
    24. using namespace std;
    25. typedef pair<int,int> PII;
    26. typedef pair<long long,long long>PLL;
    27. typedef pair<int,PII> PIII;
    28. typedef long long LL;
    29. typedef double dob;
    30. int n,m;
    31. const int N=1e6+10;
    32. int e[N],ne[N],h[N],idx;
    33. void add(int a,int b)
    34. {
    35. e[idx]=b,ne[idx]=h[a],h[a]=idx++;
    36. }
    37. int numj1,numj2;
    38. int st[N];
    39. bool ispf=0;
    40. void bfs1(int x)
    41. {
    42. queue<int>q;
    43. q.push(x);
    44. st[x]=1;
    45. numj1++;
    46. int f=1;
    47. while(q.size())
    48. {
    49. int t=q.front();
    50. q.pop();
    51. for(int i=h[t];~i;i=ne[i])
    52. {
    53. int j=e[i];
    54. if(!st[j])
    55. {
    56. q.push(j);
    57. if(st[t]==1)st[j]=2,numj2++;
    58. if(st[t]==2)st[j]=1,numj1++;
    59. }
    60. else
    61. {
    62. if(st[j]==st[t])
    63. {
    64. ispf=1;
    65. return;
    66. }
    67. }
    68. }
    69. }
    70. }
    71. int ans;
    72. signed main()
    73. {
    74. quick_cin();
    75. memset(h,-1,h);
    76. cin>>n>>m;
    77. while(m--)
    78. {
    79. int a,b;
    80. cin>>a>>b;
    81. add(a,b),add(b,a);
    82. }
    83. rep2(i,1,n)
    84. {
    85. if(!st[i])
    86. {
    87. numj1=0,numj2=0;
    88. bfs1(i);
    89. if(ispf)
    90. {
    91. cout<<-1;
    92. return 0;
    93. }
    94. ans+=max(numj1,numj2);
    95. }
    96. }
    97. cout<<ans;
    98. return 0;
    99. }

    3, Perimeter

    dfs搜索最外边周长;

    注意set的用法;

    1. #pragma GCC optimize(2)
    2. #include<bits/stdc++.h>
    3. #define rep1(i,a,n) for( int i=a;i<n;++i)
    4. #define rep2(i,a,n) for( int i=a;i<=n;++i)
    5. #define per1(i,n,a) for( int i=n;i>a;i--)
    6. #define per2(i,n,a) for( int i=n;i>=a;i--)
    7. #define quick_cin() cin.tie(0),cout.tie(0),ios::sync_with_stdio(false)
    8. #define memset(a,i,b) memset((a),(i),sizeof (b))
    9. #define memcpy(a,i,b) memcpy((a),(i),sizeof (b))
    10. #define pro_q priority_queue
    11. #define pb push_back
    12. #define pf push_front
    13. #define endl "\n"
    14. #define lowbit(m) ((-m)&(m))
    15. #define YES cout<<"YES\n"
    16. #define NO cout<<"NO\n"
    17. #define Yes cout<<"Yes\n"
    18. #define No cout<<"No\n"
    19. #define yes cout<<"yes\n"
    20. #define no cout<<"no\n"
    21. #define yi first
    22. #define er second
    23. using namespace std;
    24. typedef pair<long long,long long>PLL;
    25. typedef long long LL;
    26. typedef pair<int,int> PII;
    27. typedef pair<int,PII> PIII;
    28. typedef double dob;
    29. set<PII>s,vis;
    30. int n;
    31. int ans;
    32. bool ishole(int x,int y)
    33. {
    34. rep2(i,-1,1)
    35. rep2(j,-1,1)
    36. {
    37. if(s.count(make_pair(x+i,y+j))==1)return 0;
    38. }
    39. return 1;
    40. }
    41. int dx[]={-1,0,1,0};
    42. int dy[]={0,1,0,-1};
    43. void dfs(int x,int y)
    44. {
    45. if(s.count(make_pair(x,y)))
    46. {
    47. ans++;
    48. return;
    49. }
    50. if(vis.count(make_pair(x,y)))return;
    51. vis.insert(make_pair(x,y));
    52. if(ishole(x,y))return;
    53. rep1(i,0,4)
    54. {
    55. dfs(x+dx[i],y+dy[i]);
    56. }
    57. }
    58. signed main()
    59. {
    60. quick_cin();
    61. cin>>n;
    62. while (n--)
    63. {
    64. int x,y;
    65. cin>>x>>y;
    66. s.insert(make_pair(x,y));
    67. }
    68. auto st=*s.begin();
    69. dfs(st.first-1,st.second-1);
    70. cout<<ans;
    71. return 0;
    72. }

  • 相关阅读:
    数字化转型 — 工业数字化转型 — 工业自动化和控制系统
    用 Python 远程控制 Windows 服务器,太好用了!
    huggingface上传或发布自己的模型(大语言模型LLM)
    【GitLab CI/CD、SpringBoot、Docker】GitLab CI/CD 部署SpringBoot应用,部署方式Docker
    IntelliJ IDEA 2022.2 正式来临:已完全支持 Spring 6 和 Spring Boot 3
    医院预约小程序源码,挂号陪护就医功能,提供全方位服务
    IDEA中使用Git
    音乐管理系统
    mybatis中有哪些执行器(Executor)呢?
    大语言模型QA
  • 原文地址:https://blog.csdn.net/aidaqiudeaichao/article/details/125548641