Graph’s Cycle Component

本文介绍了一种算法,用于计算给定图中各组件的数量及其中环形图的数量。通过输入顶点数量和边的关系,算法能够找出直接或间接相连的顶点所构成的组件,并判断哪些组件构成了环形图。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

描述:

In graph theory, a cycle graph is an undirected graph that consists of a single cycle, or in other words, some number of vertices connected in a closed chain.
Now, you are given a graph where some vertices are connected to be components, can you figure out how many components are there in the graph and how many of those components are cycle graphs.
Two vertices belong to a same component if and only if those two vertices connect each other directly or indirectly.

 

输出:

The input consists of multiply test cases.
The first line of each test case contains two integer, n (0 < n < 100000), m (0 <= m <= 300000), which are the number of vertices and the number of edges.
The next m lines, each line consists of two integers, u, v, which means there is an edge between u and v.
You can assume that there is no multiply edges and no loops.
The last test case is followed by two zeros, which means the end of input.


输出:

For each test case, output the number of all the components and the number of components which are cycle graphs.


样例输出:

8 9
0 1
1 3
2 3
0 2
4 5
5 7
6 7
4 6
4 7
2 1
0 1
0 0


样例输出:

2 1
1 0


题目大意:

您将获得一些图形,其中某些顶点连接到组件,您可以确定图中有多少组件,以及这些组件中有多少是循环图。当且仅当这两个顶点直接或间接连接时,两个顶点属于相同的组件。


#include<stdio.h>
#include<string.h>
int map[110000],asd[110000];
int n,m;
int find(int x)											//查找祖先结点 
{
    return x==map[x]?x:find(map[x]);
}
void combil(int a,int b)								//建立连接使得有共同祖先 
{
    int p=find(a);
    int q=find(b);
	if(p!=q)
	{
		if(p<q)
        map[q]=p;
   		else
        map[p]=q;
	}
}
int main()
{
	int a,b,cycle,conponents;
    while(scanf("%d %d",&n,&m),n)
    {
        for(int i=1;i<=n;i++)
        map[i]=i;
        memset(asd,0,sizeof(asd));
        for(int i=1;i<=m;i++)
        {
            scanf("%d %d",&a,&b);
            asd[++a]++;											//注意要先加加 
            asd[++b]++;
            combil(a,b);
        }
        conponents=0;
        cycle=0;
        for(int i=1;i<=n;i++)
        {
	     if(map[i]==i)
             conponents++;
	}
        for(int i=1;i<=n;i++)
        {
            if(asd[i]!=2)										//如果个数不是2就一定不是组件 
            map[find(i)]=0;
        }
        for(int i=1;i<=n;i++)
	{
            if(map[i]==i)
            cycle++;			
	}
        printf("%d %d\n",conponents,cycle);
    }
    return 0;
}


private void InitializeComponent() { this.components = new System.ComponentModel.Container(); System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1)); this.button1 = new System.Windows.Forms.Button(); this.label1 = new System.Windows.Forms.Label(); this.tb_test_ID = new System.Windows.Forms.TextBox(); this.label2 = new System.Windows.Forms.Label(); this.cb_wavefoam_type = new System.Windows.Forms.ComboBox(); this.label3 = new System.Windows.Forms.Label(); this.tb_test_current_value = new System.Windows.Forms.TextBox(); this.label4 = new System.Windows.Forms.Label(); this.tb_current_station = new System.Windows.Forms.TextBox(); this.label5 = new System.Windows.Forms.Label(); this.tb_current_current = new System.Windows.Forms.TextBox(); this.label6 = new System.Windows.Forms.Label(); this.tb_current_limit_voltage = new System.Windows.Forms.TextBox(); this.label7 = new System.Windows.Forms.Label(); this.tb_fail_sample_station = new System.Windows.Forms.TextBox(); this.label10 = new System.Windows.Forms.Label(); this.label9 = new System.Windows.Forms.Label(); this.tb_pos_cycle = new System.Windows.Forms.TextBox(); this.tb_neg_cycle = new System.Windows.Forms.TextBox(); this.tb_osc_source = new System.Windows.Forms.TextBox(); this.pb_current_wavefoam = new System.Windows.Forms.PictureBox(); this.label8 = new System.Windows.Forms.Label(); this.tb_current_wavefoam_time = new System.Windows.Forms.TextBox(); this.label11 = new System.Windows.Forms.Label(); this.pb_set_logo = new System.Windows.Forms.PictureBox(); this.label12 = new System.Windows.Forms.Label(); this.label13 = new System.Windows.Forms.Label(); this.sp_connect_PLC = new System.IO.Ports.SerialPort(this.components); this.btn_connect_oplc = new System.Windows.Forms.Button(); this.label14 = new System.Windows.Forms.Label(); this.PLC_String = new System.Windows.Forms.TextBox(); this.tb_wavefoam_qty = new System.Windows.Forms.TextBox(); this.station_1_count = new System.Windows.Forms.TextBox(); this.station_2_count = new System.Windows.Forms.TextBox(); this.station_3_count = new System.Windows.Forms.TextBox(); this.station_4_count = new System.Windows.Forms.TextBox(); this.station_5_count = new System.Windows.Forms.TextBox(); this.工位1次数 = new System.Windows.Forms.Label(); this.label15 = new System.Windows.Forms.Label(); this.label16 = new System.Windows.Forms.Label(); this.label17 = new System.Windows.Forms.Label(); this.label18 = new System.Windows.Forms.Label(); this.label19 = new System.Windows.Forms.Label(); this.label20 = new System.Windows.Forms.Label(); this.tb_current_limit_voltage_neg = new System.Windows.Forms.TextBox(); this.label21 = new System.Windows.Forms.Label(); this.tb_current_current_neg = new System.Windows.Forms.TextBox(); this.label22 = new System.Windows.Forms.Label(); this.tb_limit_voltage_value = new System.Windows.Forms.TextBox(); this.label23 = new System.Windows.Forms.Label(); this.label24 = new System.Windows.Forms.Label(); this.label25 = new System.Windows.Forms.Label(); this.btn_clean_next_oder = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.textBox1 = new System.Windows.Forms.TextBox(); ((System.ComponentModel.ISupportInitialize)(this.pb_current_wavefoam)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.pb_set_logo)).BeginInit(); this.SuspendLayout();
最新发布
06-22
<think>我们正在讨论WindowsForms中的InitializeComponent方法,特别是其详细实现和控件初始化过程。根据之前的讨论和引用,我们知道InitializeComponent是设计器自动生成的方法,负责创建控件、设置属性和绑定事件。在WindowsForms中,InitializeComponent方法通常定义在窗体设计器生成的代码文件中(如Form1.Designer.cs)。该方法包含了窗体上所有控件的实例化、属性初始化和事件订阅。具体过程如下:1.创建控件实例:为窗体上的每个控件(包括窗体本身)创建实例。2.设置控件属性:包括位置(Location)、大小(Size)、文本(Text)等。3.将控件添加到父控件的Controls集合中:例如,将按钮添加到窗体的Controls集合中。4.订阅事件:将控件的事件与用户代码中定义的事件处理方法关联。由于用户要求详细,我们将深入探讨其实现机制和注意事项。注意:在WindowsForms中,InitializeComponent方法是由设计器自动维护的,因此我们不应该手动修改它,否则设计器重新生成代码时可能会覆盖手动修改。下面是一个典型的InitializeComponent方法示例(来自设计器生成的代码):</think>### Windows Forms `InitializeComponent` 方法详解 `InitializeComponent()` 是 Windows Forms 应用程序的核心初始化方法,由 Visual Studio 设计器自动生成。以下是其详细实现机制和控件初始化过程: #### 一、方法实现机制 1. **设计器代码分离** 通过 `partial class` 实现代码分离: - 用户代码:`Form1.cs` - 设计器代码:`Form1.Designer.cs`(包含 `InitializeComponent()` 实现) ```csharp public partial class Form1 : Form { // 用户编写的代码 } ``` 2. **自动生成流程** 当在设计器中拖放控件时,IDE 自动更新设计器文件: ```mermaid graph LR A[设计器操作] --> B[*.Designer.cs更新] B --> C[InitializeComponent方法生成] C --> D[控件对象创建] D --> E[属性设置] E --> F[事件绑定] ``` #### 二、控件初始化过程 1. **控件实例化** 为每个控件创建对象实例: ```csharp this.button1 = new System.Windows.Forms.Button(); this.textBox1 = new System.Windows.Forms.TextBox(); ``` 2. **属性配置** 设置控件初始状态(位置、尺寸、文本等): ```csharp // 按钮属性设置 this.button1.Location = new System.Drawing.Point(120, 80); this.button1.Size = new System.Drawing.Size(75, 23); this.button1.Text = "Submit"; this.button1.TabIndex = 0; // 文本框属性设置 this.textBox1.Location = new System.Drawing.Point(50, 30); this.textBox1.Multiline = true; ``` 3. **容器层级构建** 建立父子控件关系: ```csharp // 将控件添加到窗体容器 this.Controls.Add(this.button1); this.Controls.Add(this.textBox1); // 嵌套容器示例(如Panel) this.panel1.Controls.Add(this.label1); ``` 4. **事件绑定** 关联事件处理方法: ```csharp this.button1.Click += new System.EventHandler(this.button1_Click); this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged); ``` #### 三、内存管理机制 1. **对象生命周期** 控件树形结构的内存管理: $$ \text{Form} \rightarrow \begin{cases} \text{Controls} \rightarrow \begin{cases} \text{Button} \\ \text{Panel} \rightarrow \text{Label} \end{cases} \\ \text{MenuStrip} \rightarrow \text{MenuItem} \end{cases} $$ 2. **资源释放** 在 `Dispose()` 方法中递归释放控件资源: ```csharp protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); // 释放所有子控件 } base.Dispose(disposing); } ``` #### 四、关键注意事项 1. **调用位置** 必须在构造函数首行调用: ```csharp public Form1() { InitializeComponent(); // ← 必须第一行 // 其他初始化代码 } ``` 2. **设计器保护机制** - 方法被 `#region` 封装防止误修改 ```csharp #region Windows Form Designer generated code private void InitializeComponent() { ... } #endregion ``` - 手动修改会被设计器覆盖 3. **性能优化** 复杂窗体初始化优化策略: - 延迟加载非必要控件 - 使用 `SuspendLayout()`/`ResumeLayout()` 减少重绘 ```csharp this.SuspendLayout(); // 批量添加控件 this.ResumeLayout(false); ``` #### 五、典型问题分析 1. **空引用异常** 未调用方法时所有控件为 `null`: ```csharp public Form1() { // 忘记调用InitializeComponent button1.Text = "Test"; // NullReferenceException } ``` 2. **资源定位失败** 当项目结构变更时可能导致: ```log System.IO.FileNotFoundException: Could not find file 'obj\Debug\Form1.resx' ``` > 该方法的自动生成机制体现了 Windows Forms 框架对 RAD(Rapid Application Development) 的原生支持,显著提升开发效率[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值