题解:P11862 「o.OI R1」Easy ver.

· · 题解

题解:P11862 「o.OI R1」Easy ver.

思路

显而易见,每一个节点最少都要有一条边链接,而这条边的边权不会小于这个点的编号,也就是说除了最小的 1 每个数都至少对应有一条边边权不小于该点编号,想要让最小生成树的边权和最小,只需要保证每个点(1 除外)都相邻一个比他编号小的数即可,所以我们直接从 1 输出到 n\cdot m 即可满足要求。

代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
template<typename T>
void read(T& x){x=0;char ch=getchar();long long f=1;while(!isdigit(ch)){if(ch=='-')f*=-1;ch=getchar();}while(isdigit(ch)){x=x*10+ch-48;ch=getchar();}x*=f;}
template<typename T,typename... Args>
void read(T& first,Args&... args){read(first);read(args...);}
template<typename T>
void write(T arg){T x=arg;if (x<0){putchar('-');x=-x;}if(x>9){write(x/10);}putchar(x%10+'0');}
template<typename T,typename... Args>
void write(T arg,Args... args){write(arg);if(sizeof...(args) !=0){putchar(' ');write(args...);}}
const ll N=1e5+4;
int main(){
    ll n,m;
    read(n,m);
    for(int i=0;i<n;i++){
        for(int j=1;j<=m;j++){
            write(i*m+j);putchar(' ');
        }
        putchar('\n');
    }
    return 0;
}