2016大连网络赛 Sparse Graph

本文介绍了一种解决SparseGraph补图问题的方法,通过给定的无向图及其补图,计算从指定顶点到其余所有顶点的最短距离。采用广度优先搜索策略,遍历能够到达的点并更新最短距离。

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

Sparse Graph

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)


Problem Description
In graph theory, the complement of a graph G is a graph H on the same vertices such that two distinct vertices of H are adjacent if and only if they are not adjacent in G.

Now you are given an undirected graph G of N nodes and M bidirectional edges of unit length. Consider the complement of G, i.e., H. For a given vertex S on H, you are required to compute the shortest distances from S to all N1 other vertices.
 

 

Input
There are multiple test cases. The first line of input is an integer T(1T<35) denoting the number of test cases. For each test case, the first line contains two integers N(2N200000) and M(0M20000). The following M lines each contains two distinct integers u,v(1u,vN) denoting an edge. And S (1SN) is given on the last line.
 

 

Output
For each of T test cases, print a single line consisting of N1 space separated integers, denoting shortest distances of the remaining N1 vertices from S (if a vertex cannot be reached from S, output ``-1" (without quotes) instead) in ascending order of vertex number.
 

 

Sample Input
1 2 0 1
 

 

Sample Output
1
 
分析:对于能够到达的点依次放入队列,暴力未放入的点,可行继续放入队列即可;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=2e5+10;
const int dis[4][2]={{0,1},{-1,0},{0,-1},{1,0}};
using namespace std;
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p%mod;p=p*p%mod;q>>=1;}return f;}
int n,m,k,t,dp[maxn];
set<int>a,b[maxn],c;
queue<int>p;
int main()
{
    int i,j;
    scanf("%d",&t);
    while(t--)
    {
        bool flag=false;
        scanf("%d%d",&n,&m);
        memset(dp,-1,sizeof dp);
        rep(i,1,n)b[i].clear(),a.insert(i);
        while(m--)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            b[u].insert(v),b[v].insert(u);
        }
        scanf("%d",&m);
        p.push(m);dp[m]=0;a.erase(m);
        while(!p.empty())
        {
            c.clear();
            int u=p.front();p.pop();
            for(int x:a)if(b[x].find(u)==b[x].end())dp[x]=dp[u]+1,c.insert(x),p.push(x);
            for(int x:c)a.erase(x);
        }
        rep(i,1,n)if(i!=m)
        {
            if(flag)printf(" %d",dp[i]);
            else printf("%d",dp[i]),flag=true;
        }
        printf("\n");
    }
    //system("pause");
    return 0;
}

转载于:https://www.cnblogs.com/dyzll/p/5860597.html

# 导入需要的模块 import numpy as np import open3d as o3d # 用于读写pcd文件 from sklearn.neighbors import kneighbors_graph # 用于构建KNN图 from scipy.sparse.csgraph import connected_components # 用于找到连通域 # 读取点云数据 pc = o3d.io.read_point_cloud(r'E:\BISHE\pcd\neuvsnap_0418_154523.pcd') # 读取pcd文件 points = np.asarray(pc.points) # 转换为numpy数组 # 构建KNN图,k为邻居数,可以根据数据密度调整 k = 10 graph = kneighbors_graph(points, k, mode='connectivity', include_self=False) # 找到最大的连通域 n_components, labels = connected_components(graph, directed=False) largest_label = np.argmax(np.bincount(labels)) # 找到点数最多的标签 largest_component = points[labels == largest_label] # 筛选出对应的点 # 保存筛选后的点云数据为pcd文件 pc_filtered = o3d.geometry.PointCloud() # 创建新的点云对象 pc_filtered.points = o3d.utility.Vector3dVector(largest_component) # 设置点云数据 o3d.io.write_point_cloud(r'E:\BISHE\pcd\output1.pcd', pc_filtered) # 保存为pcd文件 # 为点云数据设置颜色 colors = np.zeros((points.shape[0], 3)) # 创建一个颜色数组,大小和点云数组一致 colors[labels == largest_label] = [0.5, 0.5, 0.5] # 将保留的点云设置为灰色 colors[labels != largest_label] = [1.0, 0.0, 0.0] # 将处理的点云设置为红色 pc.colors = o3d.utility.Vector3dVector(colors) # 将颜色数组赋值给点云对象 # 可视化点云数据 o3d.visualization.draw_geometries([pc]) # 调用open3d的可视化函数,显示点云对象这段代码降噪原理是什么
05-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值