• 寒假12 图论


    1. #define _CRT_SECURE_NO_WARNINGS 1
    2. #include
    3. #include
    4. using namespace std;
    5. int const N1 = 10010;
    6. int const N2 = 100010;
    7. int arr[N1];
    8. int x[N2], y[N2];
    9. int main()
    10. {
    11. int n, m;
    12. cin >> n >> m;
    13. for (int i = 1;i <= m;i++)
    14. {
    15. scanf("%d%d", &x[i], &y[i]);
    16. arr[x[i]]++;
    17. arr[y[i]]++;
    18. }
    19. long long ans = 0;
    20. for (int i = 1;i <= m;i++)
    21. {
    22. ans += (2 * (arr[x[i]] - 1) * (arr[y[i]] - 1));
    23. }
    24. cout << ans << endl;
    25. return 0;
    26. }

    1. #define _CRT_SECURE_NO_WARNINGS 1
    2. #include
    3. #include
    4. int n, m;
    5. using namespace std;
    6. bool use[1010][1010];
    7. int lu = 0;
    8. bool arr[1010];
    9. int cnt[1010];
    10. void dfs(int num1, int num2)
    11. {
    12. if (num1 == num2)
    13. {
    14. lu++;
    15. for (int i = 1;i <= n;i++)
    16. {
    17. if (arr[i]==1)cnt[i]++;
    18. }
    19. }
    20. else
    21. {
    22. for (int i = 1;i <= n;i++)
    23. {
    24. if (use[num1][i]==1&&arr[i]==0)
    25. {
    26. arr[i] = true;
    27. dfs(i, num2);
    28. arr[i] = false;
    29. }
    30. }
    31. }
    32. }
    33. int main()
    34. {
    35. cin >> n >> m;
    36. int num1, num2;
    37. for (int i = 1;i <= m;i++)
    38. {
    39. scanf("%d%d", &num1, &num2);
    40. use[num1][num2] = true;
    41. use[num2][num1] = true;
    42. }
    43. cin >> num1 >> num2;
    44. dfs(num1, num2);
    45. int ans = 0;
    46. for (int i = 1;i <= n;i++)
    47. {
    48. if (cnt[i] == lu)ans++;
    49. }
    50. if (ans == 0)
    51. {
    52. cout << "-1" << endl;
    53. }
    54. else
    55. {
    56. cout << ans - 1 << endl;
    57. }
    58. return 0;
    59. }

     

    1. #include
    2. using namespace std;
    3. int n, m;
    4. bool jj[1010][1010];
    5. int dp[1010];
    6. int main()
    7. {
    8. cin >> n >> m;
    9. for (int i = 1;i <= n;i++)
    10. {
    11. dp[i] = i;
    12. }
    13. for (int i = 1;i <= m;i++)
    14. {
    15. int num1, num2;
    16. cin >> num1 >> num2;
    17. jj[num1][num2] = true;
    18. }
    19. for (int i = n;i >= 1;i--)
    20. {
    21. for (int j = n;j >=1 ;j--)
    22. {
    23. if (jj[j][i])
    24. {
    25. if (dp[j] < dp[i])
    26. {
    27. dp[j] = dp[i];
    28. }
    29. }
    30. else if (jj[i][j])
    31. {
    32. if (dp[i] < dp[j])
    33. {
    34. dp[i] = dp[j];
    35. }
    36. }
    37. }
    38. }
    39. for (int i = 1;i <= n;i++)
    40. {
    41. cout << dp[i] << " ";
    42. }
    43. return 0;
    44. }
    1. #include
    2. using namespace std;
    3. int n, m;
    4. bool jj[1010][1010];
    5. int dp[1010];
    6. int dfs(int x)
    7. {
    8. if (dp[x] != 0)return dp[x];
    9. int ans=x;
    10. for (int i = 1;i <= n;i++)
    11. {
    12. if (jj[x][i])
    13. {
    14. jj[x][i] = false;
    15. if (dfs(i) > ans)
    16. {
    17. ans = dfs(i);
    18. }
    19. jj[x][i] = true;
    20. }
    21. }
    22. dp[x] = ans;
    23. return ans;
    24. }
    25. int main()
    26. {
    27. cin >> n >> m;
    28. /*for (int i = 1;i <= n;i++)
    29. {
    30. dp[i] = i;
    31. }*/
    32. for (int i = 1;i <= m;i++)
    33. {
    34. int num1, num2;
    35. cin >> num1 >> num2;
    36. jj[num1][num2] = true;
    37. }
    38. for (int i = 1;i <= n;i++)
    39. {
    40. cout<<dfs(i)<<" ";
    41. }
    42. return 0;
    43. }

    1. #include
    2. using namespace std;
    3. #include
    4. int n;
    5. int const N = 110;
    6. bool vis[N][N];
    7. queue<int>q;
    8. int ans = 0;
    9. bool use[N];
    10. void bfs()
    11. {
    12. while(ans!=n)
    13. {
    14. for (int i = 1;i <= n;i++)
    15. {
    16. if(use[i])
    17. {
    18. int flag = 0;
    19. for (int j = 1;j <= n;j++)
    20. {
    21. if (vis[j][i])
    22. {
    23. flag = 1;
    24. }
    25. }
    26. if (flag == 0)
    27. {
    28. ans++;
    29. use[i] = false;
    30. cout << i << " ";
    31. for (int j = 1;j <= n;j++)
    32. {
    33. vis[i][j] = false;
    34. }
    35. }
    36. }
    37. }
    38. }
    39. }
    40. int main()
    41. {
    42. cin >> n;
    43. for (int i = 1;i <= n;i++)
    44. {
    45. for(int j=1;j<=n;j++)
    46. {
    47. int num1;
    48. cin >> num1;
    49. if (num1 == 0)break;
    50. else
    51. {
    52. vis[i][num1] = true;
    53. }
    54. }
    55. }
    56. for (int i = 1;i <= n;i++)
    57. {
    58. use[i] = 1;
    59. }
    60. /*for (int i = 1;i <= n;i++)
    61. {
    62. for (int j = 1;j <= n;j++)
    63. {
    64. if (vis[i][j])
    65. {
    66. cout << i << "-" << j << " ";
    67. }
    68. }
    69. cout << endl;
    70. }*/
    71. bfs();
    72. return 0;
    73. }

     

    1. #include
    2. using namespace std;
    3. int const N = 210;
    4. int n;
    5. int arr[N][N];
    6. int ans = 1000000;
    7. void dfs(int u,int a)
    8. {
    9. if (u == n)
    10. {
    11. if (a < ans)ans = a;
    12. }
    13. else
    14. {
    15. for (int j = u + 1;j <= n;j++)
    16. {
    17. dfs(j, (a + arr[u][j]));
    18. }
    19. }
    20. }
    21. int main()
    22. {
    23. cin >> n;
    24. for (int i = 1;i < n;i++)
    25. {
    26. for (int j = i + 1;j <= n;j++)
    27. {
    28. cin >> arr[i][j];
    29. }
    30. }
    31. dfs(1,0);
    32. cout << ans << endl;
    33. return 0;
    34. }
    1. #include
    2. #include
    3. using namespace std;
    4. int const N = 210;
    5. int n;
    6. int arr[N][N];
    7. int dp[N];
    8. int main()
    9. {
    10. cin >> n;
    11. for (int i = 1;i < n;i++)
    12. {
    13. for (int j = i + 1;j <= n;j++)
    14. {
    15. cin >> arr[i][j];
    16. }
    17. dp[i] = 1000000000;
    18. }
    19. for (int i = n - 1;i >= 1;i--)
    20. {
    21. for (int j = i + 1;j <= n;j++)
    22. {
    23. dp[i] = min(dp[i] , arr[i][j] + dp[j]);
    24. }
    25. }
    26. cout << dp[1] << endl;
    27. return 0;
    28. }

    1. #include
    2. using namespace std;
    3. int const N = 1010;
    4. bool vis[N][N];
    5. int du[N];
    6. int n, m;
    7. int main()
    8. {
    9. cin >> n >> m;
    10. for (int i = 1;i <= m;i++)
    11. {
    12. int u, v;
    13. cin >> u>>v;
    14. vis[u][v] = true;
    15. vis[v][u] = true;
    16. du[u]++;
    17. du[v]++;
    18. }
    19. for (int i = 1;i <= n;i++)
    20. {
    21. for (int j = 1;j <= n;j++)
    22. {
    23. cout << vis[i][j] << " ";
    24. }
    25. cout << endl;
    26. }
    27. for (int i = 1;i <= n;i++)
    28. {
    29. cout << du[i] << " ";
    30. for (int j = 1;j <= n;j++)
    31. {
    32. if (vis[i][j])
    33. {
    34. cout << j << " ";
    35. }
    36. }
    37. cout << endl;
    38. }
    39. return 0;
    40. }

     

    1. #include
    2. #include
    3. using namespace std;
    4. int const N = 110;
    5. int n;
    6. bool g[N][N];
    7. bool use[N];
    8. void dfs(int u)
    9. {
    10. for (int i = 1;i <= n;i++)
    11. {
    12. if (g[u][i] && use[i] == false)
    13. {
    14. use[i] = true;
    15. dfs(i);
    16. }
    17. }
    18. }
    19. int main()
    20. {
    21. cin >> n;
    22. int ans = -1;
    23. for (int i = 1;i < n;i++)
    24. {
    25. int u, v;
    26. cin >> u >> v;
    27. g[v][u] = true;
    28. }
    29. int flag2 = 0;
    30. for (int i = 1;i <= n;i++)
    31. {
    32. flag2 = 0;
    33. memset(use, 0, sizeof(use));
    34. use[i] = true;
    35. dfs(i);
    36. for (int j = 1;j <= n;j++)
    37. {
    38. if (use[j] == false)
    39. {
    40. flag2 = 1;
    41. }
    42. }
    43. if (flag2 == 0)
    44. {
    45. ans = i;
    46. break;
    47. }
    48. }
    49. cout << ans << endl;
    50. return 0;
    51. }

  • 相关阅读:
    OpenHarmony开源软件供应链安全风险
    微信小程序快速入门
    企业需要的IOS证书
    axios 基本使用与学习
    人生规划(Flag)
    【Spark | SparkSQL】
    springboot升级过程中踩坑定位分析记录 | 京东云技术团队
    【狂神说Java】Mybatis-plus
    LiftPool:双向池化操作,细节拉满,再也不怕丢特征了 | ICLR 2021
    Perl中的单行注释和多行注释语法
  • 原文地址:https://blog.csdn.net/weixin_72770922/article/details/136106960