chen719

· · 个人记录

B3643 图的存储

#include <bits/stdc++.h>
#define int long long
#define fr(i, l, r) for(int i = l; i <= r; ++ i)
using namespace std;
int a[1010][1010];//邻接矩阵:简单,不用考虑重边与自环,缺点是空间大
signed main()
{
    int n,m;
    vector<int>v[1010];//略复杂,空间小
    cin>>n>>m;
    fr(i,1,m){
        int x,y;
        cin>>x>>y;
        a[x][y]=1;
        a[y][x]=1;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    fr(i,1,n){
        fr(j,1,n)
        cout<<a[i][j]<<' ';
        cout<<"\n";
    }fr(i,1,n)
        sort(v[i].begin(),v[i].end());
    fr(i,1,n){
        cout<<v[i].size()<<" ";
        fr(j,0,v[i].size()-1)
            cout<<v[i][j]<<' ';
        cout<<"\n";
    }
    return 0;
}

U460099 结点的度

#include <bits/stdc++.h>
#define int long long
#define fr(i, l, r) for(int i = l; i <= r; ++ i)
using namespace std;
int a[1010][1010];
signed main()
{
    int n,m;
    vector<int>v[1010];
    cin>>n>>m;
    fr(i,1,m){
        int x,y;
        cin>>x>>y;
        v[x].push_back(y);//无向图
        v[y].push_back(x);
    }
    fr(i,1,n){
        cout<<v[i].size()<<"\n";
    }
    return 0;
}

U460103 图的类型

#include <bits/stdc++.h>
#define int long long
#define fr(i, l, r) for(int i = l; i <= r; ++ i)
using namespace std;
int a[1010][1010];
signed main()
{
    int n,m;
    vector<int>v[1000010];
    cin>>n>>m;
    fr(i,1,m){
        int x,y;
        cin>>x>>y;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    int f=0,f1=0;
    fr(i,1,n){
        if(v[i].size()==1)f++;
        else if(v[i].size()==2)f1++;
    }
    if(f==2&&f1==n-2){//链图
        cout<<"Chain";
        return 0;
    }
    f=0,f1=0;
    fr(i,1,n){
        if(v[i].size()==1)f++;
        else if(v[i].size()==n-1)f1++;
    }
    if(f==n-1&&f1==1){//菊花图
        cout<<"Flower";
        return 0;
    }
    f=0,f1=0;
    fr(i,1,n){
        if(v[i].size()==2)f++;
        else if(v[i].size()==n-1)f1++;
    }
    if(f==n){//简单环
        cout<<"Ring";
        return 0;
    }
    cout<<"Neither";
    return 0;
}

B3613 图的存储与出边的排序

#include <bits/stdc++.h>
#define int long long
#define fr(i, l, r) for(int i = l; i <= r; ++ i)
using namespace std;
vector<int>v[501000];
signed main()
{
    int t;
    cin>>t;
    while(t--){
        int n,m;
        cin>>n>>m;
        fr(i,1,m){
            int x,y;
            cin>>x>>y;
            v[x].push_back(y);
        }
        fr(i,1,n){
            if(v[i].size()==0){
                cout<<"\n";
                continue; 
            }
            sort(v[i].begin(),v[i].end());
            fr(j,0,v[i].size()-1)
                cout<<v[i][j]<<" ";
            cout<<"\n";
            v[i].clear();
        }
    }
    return 0;
}

U460102 点的邻居

#include <bits/stdc++.h>
#define int long long
#define fr(i, l, r) for(int i = l; i <= r; ++ i)
using namespace std;
vector<int>v[501000];
signed main()
{
    int t;
        int n,m;
        cin>>n>>m;
        fr(i,1,m){
            int x,y;
            cin>>x>>y;
            if(find(v[x].begin(),v[x].end(),y)!=v[x].end()||x==y)continue; 
            v[x].push_back(y);
            v[y].push_back(x);
        }
        fr(i,1,n){
            if(v[i].size()==0){
                cout<<"\n";
                continue; 
            }
            sort(v[i].begin(),v[i].end());
            fr(j,0,v[i].size()-1)
                cout<<v[i][j]<<" ";
            cout<<"\n";
            v[i].clear();
        }
    return 0;
}