#216d2

A. Valera and Plates  模拟

B. Valera and Contest 构造

C. Valera and Elections

http://codeforces.com/contest/369/problem/C

我的思路是首先dfs把树建起来,然后对每条需要修的边,从离树根远的那个开始向上标记,直到根节点,这样就构成了一颗新的树,新的树中的叶子节点就是所求节点,标记时候把叶子和中间节点区分开就行了。由于n比较大,要防止退化成一条链的情况,遇到标记过的,就不再向上重复标记了。

没用过邻接表,加上开始思路不是很明确,直接卡了一小时。

# include <stdio.h>
# include <vector>

using namespace std;

# define N 100005

int n;
vector <int> vec[N];

int sz[N];
int u[N], v[N], w[N];
int fa[N];

bool vis[N];
int color[N];

void dfs(int cur)
{
    for (int i = 0; i < vec[cur].size(); ++i) {
        int v = vec[cur][i];
        if (!vis[v]) {
            vis[v] = true;
            fa[v] = cur;
            dfs(v);
        }
    }
}

int main()
{
    scanf("%d", &n);
    for (int i = 0; i < n-1; ++i) {
        scanf("%d%d%d", &u[i], &v[i], &w[i]);
        vec[u[i]].push_back(v[i]);
        vec[v[i]].push_back(u[i]);
    }
    vis[1] = true;
    dfs(1);
    for (int i = 0; i < n-1; ++i) if (w[i] == 2) {
        int cur;
        if (  fa[ u[i] ] == v[i] ) {
            cur = u[i];
        }else {
            cur = v[i];
        }
        if (color[cur] == 0) {
             color[cur] = 1;
             cur = fa[cur];
             while (color[cur] <= 1) {
                 color[cur] = 2;
                 if (cur == 1) break;
                 cur = fa[cur];
             }
        } else ++color[cur];
    }
    int cnt = 0;
    for (int i = 1; i <= n; ++i) if (color[i] == 1) { ++cnt; }
    printf("%d\n", cnt);
    for (int i = 1; i <= n; ++i) if (color[i] == 1) {
        printf("%d ", i);
    }
    printf("\n");
    
    return 0;
}

转载于:https://www.cnblogs.com/txd0u/p/3450563.html

PermissionError Traceback (most recent call last) Cell In[75], line 6 3 from d2l import torch as d2l 5 batch_size = 256 ----> 6 train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size) File E:\Anaconda\envs\d2l\lib\site-packages\d2l\torch.py:3018, in load_data_fashion_mnist(batch_size, resize) 3016 trans.insert(0, transforms.Resize(resize)) 3017 trans = transforms.Compose(trans) -> 3018 mnist_train = torchvision.datasets.FashionMNIST( 3019 root="../data", train=True, transform=trans, download=True) 3020 mnist_test = torchvision.datasets.FashionMNIST( 3021 root="../data", train=False, transform=trans, download=True) 3022 return (torch.utils.data.DataLoader(mnist_train, batch_size, shuffle=True, 3023 num_workers=get_dataloader_workers()), 3024 torch.utils.data.DataLoader(mnist_test, batch_size, shuffle=False, 3025 num_workers=get_dataloader_workers())) File E:\Anaconda\envs\d2l\lib\site-packages\torchvision\datasets\mnist.py:100, in MNIST.__init__(self, root, train, transform, target_transform, download) 97 return 99 if download: --> 100 self.download() 102 if not self._check_exists(): 103 raise RuntimeError("Dataset not found. You can use download=True to download it") File E:\Anaconda\envs\d2l\lib\site-packages\torchvision\datasets\mnist.py:180, in MNIST.download(self) 177 if self._check_exists(): 178 return --> 180 os.makedirs(self.raw_folder, exist_ok=True) 182 # download files 183 for filename, md5 in self.resources: File E:\Anaconda\envs\d2l\lib\os.py:215, in makedirs(name, mode, exist_ok) 213 if head and tail and not path.exists(head): 214 try: --> 215 makedirs(head, exist_ok=exist_ok) 216 except FileExistsError: 217 # Defeats race condition when another thread created the path 218 pass File E:\Anaconda\envs\d2l\lib\os.py:215, in makedirs(name, mode, exist_ok) 213 if head and tail and not path.exists(head): 214 try: --> 215 makedirs(head, exist_ok=exist_ok) 216 except FileExistsError: 217 # Defeats race condition when another thread created the path 218 pass File E:\Anaconda\envs\d2l\lib\os.py:225, in makedirs(name, mode, exist_ok) 223 return 224 try: --> 225 mkdir(name, mode) 226 except OSError: 227 # Cannot rely on checking for EEXIST, since the operating system 228 # could give priority to other errors like EACCES or EROFS 229 if not exist_ok or not path.isdir(name): PermissionError: [WinError 5] 拒绝访问。: &#39;../data&#39;什么意思
03-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值