2449385_wa.cpp

来自「北大大牛代码 1240道题的原代码 超级权威」· C++ 代码 · 共 95 行

CPP
95
字号
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define INIT (tree *)malloc(sizeof(tree))
int changed;
int num[100001];
int visited[100001];

typedef struct node
{
	int adj;
	struct node *next;
}tree;

typedef struct Node
{
	bool app;
	tree *head;
}Adj;
Adj adj[100001];

int n;

int dfs(int v)
{
	tree *s;

	visited[v] = 1;
	s = adj[v].head;
	while(s->next)
	{
		if(!visited[s->next->adj])
			num[v] += dfs(s->next->adj);
		s = s->next;
	}
	return num[v];
}

int main()
{
	int i, q;
	char ch[2];
	tree *s;
	int a, b;

	scanf("%d",&n);
	for(i = 0; i < n; i++)
	{
		num[i] = 1;
		adj[i].head = INIT;
		adj[i].app = true;
		adj[i].head->next = NULL;
	}
	for(i = 0; i < n-1; i++)
	{
		scanf("%d%d",&a,&b);
		a--,b--;
		s = INIT;
		s->adj = b;
		s->next = adj[a].head->next;
		adj[a].head->next = s;
		s = INIT;
		s->adj = a;
		s->next = adj[b].head->next;
		adj[b].head->next = s;
	}
	scanf("%d",&q);
	changed = 0;
	dfs(0);
	for(i = 0; i < q; i++)
	{
		scanf("%s%d",ch,&a);
		a--;
		if(ch[0]=='C')
		{
			adj[a].app = !adj[a].app;
			changed = 1;
		}
		else
		{
			if(changed)
			{
				changed = 0;
				for(i = 0; i < n; i++)
					num[i] = adj[i].app;
				memset(visited,0,sizeof(visited));
				dfs(0);
				printf("%d\n",num[a]);
			}
			else
				printf("%d\n",num[a]);
		}
	}
	return 1;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?