sgu230(拓扑排序) 231

230:

#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
const int maxn = 110;
int flag ,wi[maxn][maxn],k,t,degree[maxn],ans[maxn];
int n,m,a,b;
void init(){
    scanf("%d%d",&n,&m);
    for(int i = 1; i <= m ; i++){
        scanf("%d%d",&a,&b);
        if(wi[a][b] == 0)
            degree[b]++;
        wi[a][b] = 1;
    }
}
void sov(){
    for(int i = 1; i <= n ; i++){
        for( t = 1;t <= n ; t++){
            if(degree[t] == 0)  break;
        }
        if(t > n ){
            flag = 1;
            break;
        }
        for(int j  = 1; j <= n ; j++){
            if(wi[t][j])    degree[j]--;
        }
        ans[t] = i ;degree[t]--;
    }
    if(flag)    printf("No solution\n");
    else
        for(int i = 1; i <= n ; i++)
            printf("%d%c",ans[i],i == n+1?'\n':' ');
}
int main(){
    init();
    sov();
}

231:

#include <iostream>
using namespace std;
const int maxn = 1e6+10;
bool f[maxn];
int ok[100000],out[10000];
int n, sum, ans;
void sift (int n) {
    for (int i = 2; i <= n; i++) {
        if (!f[i]) {
            ok[++sum] = i;
            for (int j = i; j <= n; j += i)    f[j] = 1;
        }
    }
}
int main() {
    cin >> n;
    sift (n);
    for (int i = 1; i < sum; i++)
        if (!f[ok[i] + 2])        out[++ans]=ok[i];

    cout << ans << endl;
    for(int i=1;i<=ans;i++)
        cout<<2<<' '<<out[i]<<endl;
}
阅读更多
版权声明:转我原创记得说你是我的脑残粉哟 https://blog.csdn.net/zjy2015302395/article/details/77568069
个人分类: acm 基本算法
上一篇sgu223Little Kings(状压)
下一篇sgu226. Colored graph
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭