【11.13 测试】Tourist Attractions

 

 

 

 

 

题解:考场上打得一手好暴力。DFS骗得40分

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1502;
const int M=1125752;
int n;
char s[N];
struct node{
    int to;
    int next;
}e[M*2];
int head[N],cnt,vis[N];
void add(int x,int y){
    e[++cnt].to=y;
    e[cnt].next=head[x];
    head[x]=cnt;
}
ll ans;
void dfs(int now,int sum){
    if(sum==4) { ans++; return; }
    for(int i=head[now];i;i=e[i].next){
        int v=e[i].to;
        if(vis[v]==1) continue;
        vis[v]=1; dfs(v,sum+1); vis[v]=0;
    }
}
int main(){
    freopen("tour.in","r",stdin);
    freopen("tour.out","w",stdout);
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%s\n",s+1);
        for(int j=1;j<=n;j++)
            if(s[j]=='1')  add(i,j);        
    }
    for(int i=1;i<=n;i++) 
        { vis[i]=1; dfs(i,1); vis[i]=0; }
    printf("%lld",ans);
    return 0;
}

 emmm好像……70分(我希望我是70分)用“度”来思考。

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=302;
const int M=1125752;
int n,a[N][N],du[N],ans;
char s[N];
int main(){
    freopen("tour.in","r",stdin);
    freopen("tour.out","w",stdout);
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%s",s+1);
        for(int j=1;j<=n;j++)
            if(s[j]=='0') a[i][j]=0;
            else a[i][j]=1,du[i]++;
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(i==j) continue;
            if(!a[i][j]) continue;
            for(int k=1;k<=n;k++){
                if( i==k || j==k ) continue;
                if(!a[j][k]) continue;
                ans+=du[k]-1;
                if(a[k][i]) ans--;
            }
        }
    }
    printf("%d\n",ans);
    return 0;
}

 

posted @ 2019-11-13 14:48  #Cookies#  阅读(157)  评论(0编辑  收藏  举报